if(!mute.isPressed()) you forgot ! in else if .............
in both if it is mute.isPressed() ...........
and would be better
if(mute.isPressed()){
mp.setVolume(0,0);
}
else{
mp.setVolume(0, 1);
}
因为 mute.isPressed() 是一个布尔值,如果它不是真的,所以不需要检查它是否是假的............
可以在Android中使用代码ImageView Button Toggle
为你 :
final ImageView button01 = (ImageView) findViewById(R.id.button01);
button01.setOnClickListener(new OnClickListener() {
boolean isPressed = false;
public void onClick(View v) {
if (isPressed ) {
//button01.setImageResource(R.drawable.image01);.
mp.setVolume(0,0);
} else {
//button01.setImageResource(R.drawable.image02);.
mp.setVolume(0,1);
}
isPressed = !isPressed ;
}
});
..................................................... …………………………………………………………………………………………………………………………………………
alternative answer :
让我们让它变得有趣:)
button01.setOnClickListener(new OnClickListener() {
int state = 0;
public void onClick(View v) {
state = (state+1)%2;
mp.setVolume(0,state);
}
});
看起来不错 ?