Android ImageButton - 单击按钮时如何更改图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24755849/
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 - How to change the image when button is clicked?
提问by Ali
If the button was clicked I want to turn on the wifi and also change the Image of ImageButton, but I do not know how to change it
如果按钮被点击我想打开wifi并更改ImageButton的图像,但我不知道如何更改它
I have also tried this from how to change the image of a button with every click?but is isn't working:
我也尝试过如何通过每次点击更改按钮的图像?但不起作用:
boolean isPressed=false
button.setOnClickListener(buttonListener);
OnClickListener buttonListener= new OnClickListener() {
@Override
public void onClick(View v) {
if(isPressed){
button.setBackgroundResource(R.drawable.icon1);
}else{
button.setBackgroundResource(R.drawable.icon2);
}
isPressed=!isPressed;
}};
when I write the code above, android studio shows this:
Cannot resolve symbol setOnClickListener
当我编写上面的代码时,android studio 显示:无法解析符号 setOnClickListener
I have also created a button_wifi_selector xml, which it looks like this:
我还创建了一个 button_wifi_selector xml,它看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item android:drawable="@drawable/wifi_on"
android:state_pressed="true" />
<item android:drawable="@drawable/ic_launcher"
android:state_focused="true" />
<item android:drawable="@drawable/wifi" />
</selector>
and in my activity i have this
在我的活动中,我有这个
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton_wifi"
android:layout_below="@+id/toggleButton_wifi"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="turnOffWifiDemo"
android:src="@drawable/button_wifi_selector" />
but it isn't doing, what I want Can somebody pls help me? Thanks
但它没有做,我想要什么有人可以帮助我吗?谢谢
EDIT: it works with the first code. I just had to remove the onClick from ImageButton in the xml BUT: he is changing the picture the second time, when I start the app. After that he changes it every time
编辑:它适用于第一个代码。我只需要从 xml 中的 ImageButton 中删除 onClick 但是:当我启动应用程序时,他第二次更改图片。之后他每次都换
回答by reza.abdi
this code work for me; test it
这段代码对我有用;测试一下
final ImageButton btnTest =(ImageButton) findViewById(R.id.btnexctract);
btnTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
btnTest.setSelected(!btnextra.isPressed());
if (btnTest.isPressed()) {
btnextra.setImageResource(R.drawable.yourImage);
}
else {
btnTest.setImageResource(R.drawable.yourImage2);
}
}
});
回答by Badrul
This is how you have to do it
这就是你必须做的
Selector xml:
选择器 xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="@drawable/info_icon_solid_with_shadow" />
<item
android:drawable="@drawable/info_icon_outline_with_shadow" />
</selector>
And then in java:
然后在java中:
//assign the image in code (or you can do this in your layout xml)
imageButton.setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable....));
//set the click listener
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View button) {
//Set the button's appearance
button.setSelected(!button.isSelected());
if (button.isSelected()) {
//Handle selected state change
}
else {
//Handle de-select state change
}
}
});
回答by Meghna
I have modified it,for on/off it may help you
我已经修改了它,打开/关闭它可能会帮助你
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="@+id/imageButton_wifi2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickEvent"
android:visibility="invisible"
android:src="@drawable/on" />
<ImageButton
android:id="@+id/imageButton_wifi1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickEvent"
android:src="@drawable/off" />
</FrameLayout>
like you need to create method with view parameter
就像您需要使用视图参数创建方法一样
{
c=false;
im1=(ImageButton)findViewById(R.id.imageButton_wifi1);
im2=(ImageButton)findViewById(R.id.imageButton_wifi2);
}
public void clickEvent(View v)
{
//Code to implement
if(c) {
im2.setVisibility(View.VISIBLE);
im1.setVisibility(View.INVISIBLE);
c=false;
}
else {
im1.setVisibility(View.VISIBLE);
im2.setVisibility(View.INVISIBLE);
c=true;
}
回答by ADT
You can use togglebutton like this
您可以像这样使用切换按钮
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="74dp"
android:layout_height="26dp"
android:background="@drawable/toggle"
android:checked="false"
android:paddingRight="10dp"
android:text="ON"
android:textColor="#ffffff"
android:textSize="13sp"
android:textStyle="bold" />
And in java file
在java文件中
final ToggleButton toggleButton=(ToggleButton)findViewById(R.id.toggleButton1);
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
//
toggleButton.setBackgroundResource(R.drawable.icon1);
}else{
//
toggleButton.setBackgroundResource(R.drawable.icon2);
}
}
});
回答by cantas
You can define a boolean flag and just change visibility of items. Description: changeBack is a button. This code is for change images Your onClick does not know which id is clicked?
您可以定义一个布尔标志并更改项目的可见性。描述:changeBack 是一个按钮。此代码用于更改图像您的 onClick 不知道单击了哪个 ID?
private boolean bgcolor = false;
public void onClick(View view) {
switch(view.getId())
{
case R.id.changeBack:
bgcolor = !bgcolor;
if (bgcolor) {
imv.setVisibility(View.VISIBLE);
imv2.setVisibility(View.INVISIBLE);
}
else {
imv2.setVisibility(View.VISIBLE);
imv.setVisibility(View.INVISIBLE);
}
break;
}
}