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()是一個布爾值,如果它是不是真的那麼不需要檢查它是假的.........
可以使用代碼ImageView Button Toggle in Android
你:
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);
}
});
看起來不錯?
你不需要在if語句中使用大括號,否則if語句即if(mute.isPressed()){mp.setVolume(0,0);} ...? – dshapiro
看看丹尼爾的評論,而且你在if和else if中檢查相同的條件。 – Orlymee
http://stackoverflow.com/questions/4573667/how-to-mute-mediaplayer-in-android –