Android 使用不同状态的两个图像切换按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11499574/
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
Toggle button using two image on different state
提问by MBMJ
I need to make a toggle button using two image instead of ON/OFF state.
我需要使用两个图像而不是 ON/OFF 状态来制作一个切换按钮。
At off state i set a background image.But the OFF text can not removed while i use background image.
在关闭状态下,我设置了一个背景图像。但是当我使用背景图像时无法删除关闭文本。
And i can not set another image on ON state by clicking the toggle button :( I am new in android. I hope you guys help me get out of this problem
而且我无法通过单击切换按钮将另一个图像设置为 ON 状态:( 我是 android 新手。我希望你们帮我解决这个问题
回答by AkashG
Do this:
做这个:
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/check" <!--check.xml-->
android:layout_margin="10dp"
android:textOn=""
android:textOff=""
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_centerVertical="true"/>
create check.xml in drawable folder
在 drawable 文件夹中创建 check.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/selected_image"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/unselected_image"
android:state_checked="false"/>
</selector>
回答by MistaGreen
AkashG's solution don't work for me. When I set up check.xml to background it's just stratched in vertical direction. To solve this problem you should set up check.xml to "android:button" property:
AkashG 的解决方案对我不起作用。当我将 check.xml 设置为背景时,它只是在垂直方向上分层。要解决此问题,您应该将 check.xml 设置为“android:button”属性:
<ToggleButton
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/check" //check.xml
android:background="@null"/>
check.xml:
检查.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/selected_image"
android:state_checked="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/unselected_image"
android:state_checked="false"/>
</selector>
回答by Srishti Roy
You can try something like this. Here on click of image button I toggle the imageview.
你可以尝试这样的事情。在单击图像按钮时,我会切换图像视图。
holder.imgitem.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(!onclick){
mSparseBooleanArray.put((Integer) view.getTag(), true);
holder.imgoverlay.setImageResource(R.drawable.ipad_768x1024_editmode_delete_overlay_com);
onclick=true;}
else if(onclick)
{
mSparseBooleanArray.put((Integer) view.getTag(), false);
holder.imgoverlay.setImageResource(R.drawable.ipad_768x1024_editmode_selection_com);
onclick=false;
}
}
});