如果条件,如何禁用按钮单击.下面的代码不起作用.它会停止应用程序


@Override
public void onClick(View view) {

switch (view.getId()){
case R.id.next:
count++;
image1.setImageResource(firstArray[count]);
image2.setImageResource(secondArray[count]);
image3.setImageResource(thirdArray[count]);

first.setText(first_tv[count]);
if (count == 0){
forward.setClickable(false);
forward.setVisibility(View.GONE);
//                    forward.setEnabled(false);
}

在你的 ButtonClick,

switch (view.getId()){
case R.id.next:
count++;
image1.setImageResource(firstArray[count]);
image2.setImageResource(secondArray[count]);
image3.setImageResource(thirdArray[count]);
first.setText(first_tv[count]);
if (count == 0){
forward.setEnabled(false);
}
break;

另外,检查您的按钮单击下一步是否有效。

做这样的事情。button.setOnClickListener(null);将删除任何侦听器,并再次设置它,请使用button.setOnClickListener(click);分配它

View.OnClickListener click = new View.OnClickListener(){
@Override
public void onClick(View view){
if(someCondition){
//do something 
button.setOnClickListener(null);
}else{
//do something
button.setOnClickListener(this);
}
}
};
//some code and then
button.setOnClickListener(click);

计数的默认值是多少? 如果计数 = -1;

int count = -1; 
@Override
public void onClick(View view) {

switch (view.getId()){
case R.id.next:
count++;
image1.setImageResource(firstArray[count]);
image2.setImageResource(secondArray[count]);
image3.setImageResource(thirdArray[count]);

first.setText(first_tv[count]);
if (count == 0){
//       forward.setVisibility(View.GONE); // for Hide the button
forward.setEnabled(false); // for not clickable 
}

如果计数 = 0;

int count = 0; 
@Override
public void onClick(View view) {

switch (view.getId()){
case R.id.next:
if (count == 0){
//       forward.setVisibility(View.GONE); // for Hide the button
forward.setEnabled(false); // for not clickable
}
count++;
image1.setImageResource(firstArray[count]);
image2.setImageResource(secondArray[count]);
image3.setImageResource(thirdArray[count]);

first.setText(first_tv[count]);

最新更新