我不明白为什么 Android Studio 告诉我分配给 isOne 的值从未被使用过。我已在 fade 方法的 if 语句中将值设置为 false 和 true。然而,当我将变量 isOne 声明为成员变量而不是局部变量时,错误消失了,并且似乎工作正常。我不知道为什么会修复这个错误......有什么想法吗?
private ImageView img1;
private ImageView img2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img1 = (ImageView) findViewById(R.id.porsche1);
img2 = (ImageView) findViewById(R.id.porsche2);
img1.setOnClickListener(this);
img2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
fade();
}
public void fade(){
boolean isOne = true;
if (isOne) {
img1.animate().alpha(0).setDuration(2000);
img2.animate().alpha(1).setDuration(2000);
isOne = false;
} else {
img1.animate().alpha(1).setDuration(2000);
img2.animate().alpha(0).setDuration(2000);
isOne = true;
}
}
}
最佳答案
使用这个方法,希望对你有帮助
boolean isOne = false; // Use Globally
public void fade(){
if (isOne) {
img1.animate().alpha(0).setDuration(2000);
img2.animate().alpha(1).setDuration(2000);
} else {
img1.animate().alpha(1).setDuration(2000);
img2.animate().alpha(0).setDuration(2000);
}
}
关于java - 从未使用分配的 boolean 值 - Java/Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50033100/