CheckedTextView 类似checkbox,单选-多选。
但它支持文字,名字可以看出它继承TextView ,多了一个选择勾选框。
使用,选中未选中:
checkedTextView.toggle();
这个是一个判断源码:
public void toggle() {
setChecked(!mChecked);
}
点击事件需要我们代码捕获,代码设置,切换状态,控件本身不支持自动切换,应该没有自动捕获点击事件。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
checkedTextViewMul1 = (CheckedTextView) this
.findViewById(R.id.checkedTextView1);
View.OnClickListener checkedTextViewMulListenerRef = new View.OnClickListener() {
public void onClick(View arg0) {
CheckedTextView checkedTextView= ((CheckedTextView) arg0);
checkedTextView.toggle();
Log.d("lihui","CheckedTextView---- "+checkedTextView.isChecked());
}
};
checkedTextViewMul1.setOnClickListener(checkedTextViewMulListenerRef);
打钩--true
空白--false
}
布局:
1、复选框
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
<CheckedTextView
android:id="@+id/checktv_title"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
2、单选框
android:checkMark="?android:attr/listChoiceIndicatorSingle"
<CheckedTextView
android:id="@+id/checkedTextViewa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:tag="A"
android:text="checkedTextViewa" />
3、如果你都什么都不指定, 没有选择框。