Android Imagebutton 更改 Image OnClick
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12249495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Imagebutton change Image OnClick
提问by user1205415
I just added a new drawable
folder under res
folder. In the drawable
folder, i copied the ic_launcher.png
file from drawable-hdpi
folder.
I wanna change the standard ImageButton
image through the new one when i press the button. I wrote some code, but when i start the app, it crashes.
我刚刚drawable
在res
文件夹下添加了一个新文件夹。在drawable
文件夹中,我ic_launcher.png
从drawable-hdpi
文件夹中复制了文件。ImageButton
当我按下按钮时,我想通过新的图像更改标准图像。我写了一些代码,但是当我启动应用程序时,它崩溃了。
Button imgButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.imgButton).setOnClickListener(imgButtonHandler);
}
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
imgButton.setBackgroundResource(R.drawable.ic_launcher);
}
};
EDIT: I changed to this, and this also not works.
编辑:我改为这个,这也不起作用。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgButton = (Button) findViewById(R.id.imgButton);
imgButton.setOnClickListener(imgButtonHandler);
}
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
imgButton.setBackgroundResource(R.drawable.ic_launcher);
}
};
EDIT 2: THIS WORKS. Thanks to all.
编辑 2:这有效。谢谢大家。
ImageButton button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button= (ImageButton)findViewById(R.id.imgButton);
button.setOnClickListener(imgButtonHandler);
}
View.OnClickListener imgButtonHandler = new View.OnClickListener() {
public void onClick(View v) {
button.setBackgroundResource(R.drawable.ic_launcher);
}
};
回答by Yassine Souabni
This misled me a bit - it should be setImageResource
instead of setBackgroundResource
:) !!
这有点误导了我 - 它应该setImageResource
而不是setBackgroundResource
:) !!
The following works fine :
以下工作正常:
ImageButton btn = (ImageButton)findViewById(R.id.imageButton1);
btn.setImageResource(R.drawable.actions_record);
while when using the setBackgroundResource
the actual imagebutton's image
stays while the background image is changed which leads to a ugly looking imageButton object
而当使用setBackgroundResource
实际的图像按钮的图像时,背景图像被改变,这导致一个难看的图像按钮对象
Thanks.
谢谢。
回答by mismanc
<ImageButton android:src="@drawable/image_btn_src" ... />
image_btn_src.xml
image_btn_src.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/icon_pressed"/>
<item android:state_pressed="false" android:drawable="@drawable/icon_unpressed"/>
</selector>
回答by Carnal
That is because imgButton is null. Try this instead:
那是因为 imgButton 为空。试试这个:
findViewById(R.id.imgButton).setBackgroundResource(R.drawable.ic_action_search);
or much easier to read:
或更容易阅读:
imgButton = (Button) findViewById(R.id.imgButton);
imgButton.setOnClickListener(imgButtonHandler);
then in onClick: imgButton.setBackgroundResource(R.drawable.ic_action_search);
然后在onClick中: imgButton.setBackgroundResource(R.drawable.ic_action_search);
回答by John Smith
You can do it right in your XML file:
你可以在你的 XML 文件中做到这一点:
android:onClick="@drawable/ic_action_search"
回答by hsz
You have assing button to your imgButton
variable:
你有你的imgButton
变量assing 按钮:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgButton = (Button) findViewById(R.id.imgButton);
imgButton.setOnClickListener(imgButtonHandler);
}
回答by Oksana Kryvenko
To switch between different images when the ImageButton is clicked I used a boolean like this:
要在单击 ImageButton 时在不同图像之间切换,我使用了这样的布尔值:
ImageButton imageButton;
boolean buttonOn;
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!buttonOn) {
buttonOn = true;
imageButton.setBackground(getResources().getDrawable(R.drawable.button_is_on));
} else {
buttonOn = false;
imageButton.setBackground(getResources().getDrawable(R.drawable.button_is_off));
}
}
});