请解释我为什么代码不起作用。谢谢你的帮助。
private boolean emotico = false;
ImageView emot = (ImageView) findViewById(R.id.obrazek);
emot.setClickable(true);
emot.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
ImageView emotico = (ImageView) findViewById(R.id.obrazek);
if (emot) {
emotico.setImageResource(R.drawable.ic2);
} else {
emotico.setImageResource(R.drawable.ic1);
}
emot = !emot;
}
}
答案 0 :(得分:1)
此代码适用于您:
//emotico is a boolean variable
private boolean emotico = false;
//emot is a ImageView variable
ImageView emot = (ImageView) findViewById(R.id.obrazek);
emot.setClickable(true);
emot.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
//Two errors in your below line:
//1. 'emotico' is a boolean and 'emot' being a ImageView variable can't be used together
//2. You have already defined and instantiated it above, so no need to do it again
//ImageView emotico = (ImageView) findViewById(R.id.obrazek);
//we check for boolean conditions
if (emotico) {
//ImageView variable should be assigned here
emot.setImageResource(R.drawable.ic2);
} else {
//ImageView variable should be assigned here
emot.setImageResource(R.drawable.ic1);
}
//check for boolean condition here
emotico = !emotico;
}
}
答案 1 :(得分:0)
你正在混淆的东西,你正试图再次映射imageview的onClickListener(),请试试这个!
private boolean emotico = false;
ImageView emot = (ImageView) findViewById(R.id.obrazek);
emot.setClickable(true);
emot.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
if (emotico) {
emot.setImageResource(R.drawable.ic2);
} else {
emot.setImageResource(R.drawable.ic1);
}
emotico = !emotico ;
}
}
试试这个代码!希望它能解决你的问题! :)
答案 2 :(得分:0)
我认为您在emot(ImageView)
emotico(boolean flag)
与onClick()
混为一谈
private boolean emotico = false;
final ImageView emot = (ImageView) findViewById(R.id.obrazek);
emot.setClickable(true);
emot.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
emotico = !emotico;
if (emotico) {
emot.setImageResource(R.drawable.ic2);
} else {
emot.setImageResource(R.drawable.ic1);
}
}
}