我使用此代码,以便当我按下播放按钮时,它将更改为停止按钮。当我在显示停止按钮时按下它时,如何将其更改回播放按钮?
final ImageView Play_button = (ImageView)findViewById(R.id.playbutton);
Play_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Play_button.setImageResource(R.drawable.stopicon);
}
});
最佳答案
在这种情况下,您需要使用 if
...else
条件,我还建议您使用 boolean
变量来检查是否正在播放(如果您正在使用媒体播放器,那么您还可以使用媒体播放器类的 isPlaying()
方法)。但为了方便起见,我建议采用以下技术。
final ImageView Play_button = (ImageView)findViewById(R.id.playbutton);
boolean isPlaying = false;
Play_button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(isPlaying){
//You can add you code to change the icon to stop mode
//and finally set the flag to false as it has stopped now
isPlaying = false;
}else {
//Add you code here to change the icon back to play mode
//and finally set the flag to true as it will be playing now.
isPlaying = true;
}
}
});
关于java - Android更改播放按钮停止和停止按钮播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23745456/