在android中定义自定义复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20015463/
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
Defining custom-checkbox in android
提问by smriti3
How to make a custom check-Box in android
如何在android中制作自定义复选框
my current XML::
我目前的 XML::
<LinearLayout
android:id="@+id/linearLayout_individualdays"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_below="@+id/linearLayout_type_of_days"
android:gravity="center|top"
android:orientation="horizontal"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:visibility="gone" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mon" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tue" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wed" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Thu" />
</LinearLayout>
Out-Put::
输出::
But How to make something like below::
但是如何制作类似下面的东西::
- here blue border shows its selected
- else its not selected
- It has to be a check-box
- 这里蓝色边框显示其选定
- 否则它没有被选中
- 它必须是一个复选框
Hope i am clear !
希望我清楚!
回答by zohreh
use this code
使用此代码
select.xml in drawable folder
可绘制文件夹中的 select.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffffff" >
</solid>
<stroke
android:width="2dp"
android:color="#ff0000" >
</stroke>
<corners android:radius="5dp" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
deselect.xml in drawable folder
在drawable文件夹中取消select.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ffffff" >
</solid>
<stroke
android:width="2dp"
android:color="#000000" >
</stroke>
<corners android:radius="5dp" />
<padding
android:bottom="4dp"
android:left="4dp"
android:right="4dp"
android:top="4dp" />
</shape>
and custom checkbox
和自定义复选框
public class checkbox extends CheckBox{
public checkbox(Context context, AttributeSet attrs) {
super(context, attrs);
//setButtonDrawable(new StateListDrawable());
}
@Override
public void setChecked(boolean t){
if(t)
{
this.setBackgroundResource(R.drawable.select);
}
else
{
this.setBackgroundResource(R.drawable.deselect);
}
super.setChecked(t);
}
}
checkbox
复选框
<com.example.checkbox.checkbox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:checked="true"
android:text="checked" />
you can change color in select.xml and deselect.xml to thing that you want
您可以将 select.xml 和 deselect.xml 中的颜色更改为您想要的
回答by TheFlash
For your requirement I prefer you to use CheckedTextView instead of CheckBox.Here is the code what you wanted.
对于您的要求,我更喜欢您使用 CheckedTextView 而不是 CheckBox。这是您想要的代码。
<CheckedTextView
android:id="@+id/ctv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mon"
android:textSize="22sp"
android:layout_margin="30dp"
android:checked="true"
android:background="@drawable/chk_indicator"
android:padding="15dp"
/>
create 3 xml ( chk_indicator.xml , chk_bg.xml , chk_pressed_bg.xml )in drawable folder
在 drawable 文件夹中创建 3 个 xml( chk_indicator.xml 、 chk_bg.xml 、 chk_pressed_bg.xml )
chk_indicator.xml
chk_indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked ="true"
android:drawable="@drawable/chk_pressed_bg" />
<item android:drawable="@drawable/chk_bg" />
</selector>
chk_bg.xml
chk_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<solid android:color="#ffffff"/>
<stroke android:width="3dp"
android:color="#ababab"/>
<corners android:radius="10dp"/>
</shape>
</item>
</layer-list>
chk_pressed_bg.xml
chk_pressed_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle" >
<solid android:color="#ffffff"/>
<stroke android:width="3dp"
android:color="#5e9eff"/>
<corners android:radius="10dp"/>
</shape>
</item>
</layer-list>
output:
输出:
set onClick event on CheckedTextView
在 CheckedTextView 上设置 onClick 事件
((CheckedTextView)findViewById(R.id.ctv)).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
boolean isChecked = ((CheckedTextView)findViewById(R.id.ctv)).isChecked();
if(isChecked)
((CheckedTextView)findViewById(R.id.ctv)).setChecked(false);
else
((CheckedTextView)findViewById(R.id.ctv)).setChecked(true);
}
});
回答by Gunaseelan
<CheckBox
android:id="@+id/DinnerRG_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:background="@drawable/yourbuttonbackground"
android:button="@android:color/transparent"
android:gravity="center_horizontal"
android:padding="5dp"
android:text="Wed"
android:textSize="15dp" />
Try like this.
像这样尝试。
回答by Tony
Adding to zohreh's answer, you could get the checkbox with text only and get rid of the checkbox icon as shown below,
添加到zohreh 的答案中,您可以获得仅带有文本的复选框并删除复选框图标,如下所示,
For this, set the button drawable to null in the constructor of the custom checkbox class, as shown below,
为此,在自定义复选框类的构造函数中将按钮 drawable 设置为 null,如下所示,
public MyCheckBox(Context context, AttributeSet attrs) {
super(context, attrs);
// Don't show the checkbox.
setButtonDrawable(null);
}