CheckedTextView 使用

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、如果你都什麼都不指定, 沒有選擇框。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章