Android用java更改按钮背景图像?
我试图有一个按钮,静音的应用程序中的声音,现在工作。我的新问题是,我希望在IF语句中更改图像 布局中的按钮代码:Android用java更改按钮背景图像?,android,Android,我试图有一个按钮,静音的应用程序中的声音,现在工作。我的新问题是,我希望在IF语句中更改图像 布局中的按钮代码: <Button android:id="@+id/mute" android:background="@drawable/sound" android:layout_width="52dp" android:layout_height="52dp" andro
<Button
android:id="@+id/mute"
android:background="@drawable/sound"
android:layout_width="52dp"
android:layout_height="52dp"
android:layout_gravity="right"
/>
我需要为每个IF添加代码,一个用于显示sound on(soundon.png)按钮背景,另一个用于显示sound off(soundoff.png)按钮背景图像。(按钮ID为“静音”)
非常感谢 您可以使用setBackgroundResource更改背景
只需更改您按下的按钮的图像。不管图像在哪里,你按下的按钮在哪里?您可以显示
按钮
。因为我在上面的示例代码中没有看到它。布局中的按钮代码已更新,谢谢。。我以前试着简单地用diff image添加这一行,但没有按我的方式添加?好吧,你赢了我-但是是的@Droidster只需将此作为一个起点,即可在按下该按钮时正确更改图像。除非确保您执行按钮btn=this.findViewById(R.id.mute)
,因为他这样做的方式需要您从onCreate
将btn
设置为全局变量,这也很好。但请记住这一点。我假设您在单独的类中有图像更改代码。这就是我为什么这么说。谢谢安迪和斯奎斯奎尔!
if (cmute == false){
Editor editor = getPrefs.edit();
editor.putBoolean("mute", true);
editor.commit();
Editor editor2 = getPrefs.edit();
editor.putBoolean("notice", true);
editor.commit();
}
if (cmute == true){
Editor editor = getPrefs.edit();
editor.putBoolean("mute", false);
editor.commit();
Editor editor2 = getPrefs.edit();
editor.putBoolean("notice", false);
editor.commit();
}
Button btn = findViewById(R.id.mute);
btn.setBackgroundResource(R.drawable.soundoff);