如何以编程方式更改按钮图像play
和stop
?
public void eventOccured(int id) {
Log.i("sat", "Clicked on " + id);
if (id==4){
if (sound_Off){
sound_Off= false;
if (editor != null) {
editor.putBoolean("prefSoundOnOff",false);
editor.commit(); // Very important to save the preference
}
} else {
sound_Off= true;
if (editor != null) {
editor.putBoolean("prefSoundOnOff",true);
editor.commit(); // Very important to save the preference
}
}
}
}
});
使用findViewById()
获取imageview并将其转换为imageview
然后你可以在if语句中设置图像。
ImageView imgview = (ImageView)findViewById(R.id.myImageViewId);
imgView.setImageResource(R.id.myicon);
其中myImageViewId
是imageview的ID,设置在您的xml布局文件
在drawables中创建一个选择器,每个图像都有true和false选项,然后使用setImageResource
将选择器应用于imageview编辑:例子我看到在其他线程https://stackoverflow.com/a/14024007/3419242