我在此代码的末尾收到上述错误:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button= (ImageButton)findViewById(R.id.imgButton);
button.setOnClickListener(imgButtonHandler);
button1= (ImageButton)findViewById(R.id.imgButton1);
button1.setOnClickListener(imgButtonHandler1);
button2= (ImageButton)findViewById(R.id.imgButton2);
button2.setOnClickListener(imgButtonHandler2);
button3= (ImageButton)findViewById(R.id.imgButton3);
button3.setOnClickListener(imgButtonHandler3);
}
这是其后的代码:
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
button.setImageResource(R.drawable.whiteon);
button1.setImageResource(R.drawable.redoff);
button2.setImageResource(R.drawable.yellowoff);
button3.setImageResource(R.drawable.greenoff);
TextView text = (TextView) findViewById(R.id.textViewcategory);
text.setText("WHITE (Hi-Po)");
}
};
View.OnClickListener imgButtonHandler1 = new View.OnClickListener() {
public void onClick(View v) {
button1.setImageResource(R.drawable.redon);
button.setImageResource(R.drawable.whiteoff);
button2.setImageResource(R.drawable.yellowoff);
button3.setImageResource(R.drawable.greenoff);
TextView text = (TextView) findViewById(R.id.textViewcategory);
text.setText("RED (Significant)");
}
};
View.OnClickListener imgButtonHandler2 = new View.OnClickListener() {
public void onClick(View v) {
button2.setImageResource(R.drawable.yellowon);
button.setImageResource(R.drawable.whiteoff);
button1.setImageResource(R.drawable.redoff);
button3.setImageResource(R.drawable.greenoff);
TextView text = (TextView) findViewById(R.id.textViewcategory);
text.setText("YELLOW (Serious)");
}
};
View.OnClickListener imgButtonHandler3 = new View.OnClickListener() {
public void onClick(View v) {
button3.setImageResource(R.drawable.greenon);
button.setImageResource(R.drawable.whiteoff);
button1.setImageResource(R.drawable.redoff);
button2.setImageResource(R.drawable.yellowoff);
TextView text = (TextView) findViewById(R.id.textViewcategory);
text.setText("GREEN (Important)");
}
};
我已尝试按要求删除令牌,但是当我这样做时,它会说所有" imgButtonHandler" s无法解析为变量。
非常感谢任何帮助。
非常感谢
答案 0 :(得分:1)
请尝试这种方式,希望这有助于您解决问题。
public class MainActivity extends Activity {
private TextView text;
private ImageButton button;
private ImageButton button1;
private ImageButton button2;
private ImageButton button3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.textViewcategory);
button= (ImageButton)findViewById(R.id.imgButton);
button.setOnClickListener(imgButtonHandler);
button1= (ImageButton)findViewById(R.id.imgButton1);
button1.setOnClickListener(imgButtonHandler1);
button2= (ImageButton)findViewById(R.id.imgButton2);
button2.setOnClickListener(imgButtonHandler2);
button3= (ImageButton)findViewById(R.id.imgButton3);
button3.setOnClickListener(imgButtonHandler3);
}
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
button.setImageResource(R.drawable.whiteon);
button1.setImageResource(R.drawable.redoff);
button2.setImageResource(R.drawable.yellowoff);
button3.setImageResource(R.drawable.greenoff);
text.setText("WHITE (Hi-Po)");
}
};
View.OnClickListener imgButtonHandler1 = new View.OnClickListener() {
public void onClick(View v) {
button1.setImageResource(R.drawable.redon);
button.setImageResource(R.drawable.whiteoff);
button2.setImageResource(R.drawable.yellowoff);
button3.setImageResource(R.drawable.greenoff);
text.setText("RED (Significant)");
}
};
View.OnClickListener imgButtonHandler2 = new View.OnClickListener() {
public void onClick(View v) {
button2.setImageResource(R.drawable.yellowon);
button.setImageResource(R.drawable.whiteoff);
button1.setImageResource(R.drawable.redoff);
button3.setImageResource(R.drawable.greenoff);
text.setText("YELLOW (Serious)");
}
};
View.OnClickListener imgButtonHandler3 = new View.OnClickListener() {
public void onClick(View v) {
button3.setImageResource(R.drawable.greenon);
button.setImageResource(R.drawable.whiteoff);
button1.setImageResource(R.drawable.redoff);
button2.setImageResource(R.drawable.yellowoff);
text.setText("GREEN (Important)");
}
};
}
答案 1 :(得分:-3)
更改后面的代码,使其看起来像:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.R.id.imgButton3:
button3.setImageResource(R.drawable.greenon);
button.setImageResource(R.drawable.whiteoff);
button1.setImageResource(R.drawable.redoff);
button2.setImageResource(R.drawable.yellowoff);
TextView text = (TextView) findViewById(R.id.textViewcategory);
text.setText("GREEN (Important)");
break;
case R.id.R.id.imgButton2:
button2.setImageResource(R.drawable.yellowon);
button.setImageResource(R.drawable.whiteoff);
button1.setImageResource(R.drawable.redoff);
button3.setImageResource(R.drawable.greenoff);
TextView text = (TextView) findViewById(R.id.textViewcategory);
text.setText("YELLOW (Serious)");
break;
case R.id.R.id.imgButton1:
button1.setImageResource(R.drawable.redon);
button.setImageResource(R.drawable.whiteoff);
button2.setImageResource(R.drawable.yellowoff);
button3.setImageResource(R.drawable.greenoff);
TextView text = (TextView) findViewById(R.id.textViewcategory);
text.setText("RED (Significant)");
break;
case R.id.R.id.imgButton:
button.setImageResource(R.drawable.whiteon);
button1.setImageResource(R.drawable.redoff);
button2.setImageResource(R.drawable.yellowoff);
button3.setImageResource(R.drawable.greenoff);
TextView text = (TextView) findViewById(R.id.textViewcategory);
text.setText("WHITE (Hi-Po)");
break;
}
}