AppCompatCheckedTextView是繼承自CheckedTextView的,在Androidx中進行支持。CheckedTextView可以看成是CheckBox、RadioGroup、TextView的組合使用,可以顯示文字的同時進行單選或者複選。
當我們配合CheckedTextView使用ListView(RecyclerView不支持該方法的使用)時,使用setChoiceMode設置CHOICE_MODE_SINGLE或者CHOICE_MODE_MULTIPLE時即將列表選項設置爲了單選或者多選列表。
List<String> mData = new ArrayList<>();
for (int i = 0; i < 5; i++) {
mData.add("item" + i);
}
ListView listView = findViewById(R.id.lv_single);
//通過listView設置模式 CHOICE_MODE_SINGLE(單選模式)
listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
// 或者使用系統默認的佈局 ,佈局只決定顯示樣式,不決定是是單選還是複選模式
// android.R.layout.simple_list_item_single_choice 顯示的是圓的選擇框
// android.R.layout.simple_list_item_multiple_choice 顯示的是方框的選擇框
ArrayAdapter adapter = new ArrayAdapter(this, R.layout.item_checked_textview);
adapter.addAll(mData);
listView.setAdapter(adapter);
// activity_main.xml文件如下:
<ListView
android:id="@+id/lv_single"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
item_checked_textview.xml文件如下:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:checkMark="@drawable/selector_checked_textview"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/blue_trans" />
<!-- android:checkMark="?android:attr/listChoiceIndicatorSingle" 顯示的是圓形風格勾選框 -->
<!-- android:checkMark="?android:attr/listChoiceIndicatorMultiple" 顯示的是方形風格勾選框 -->
<!-- android:textAppearance="?android:attr/textAppearanceListItemSmall" 文字風格 -->
<!-- android:textColor="?android:attr/textColorAlertDialogListItem" 文字的顏色-->
selector_checked_textview.xml文件如下:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/checked" android:state_selected="true" />
<item android:drawable="@drawable/checked" android:state_pressed="true" />
<item android:drawable="@drawable/checked" android:state_checked="true" />
<item android:drawable="@drawable/unchecked" />
</selector>
drawable="@drawable/unchecked"和drawable="@drawable/checked"用的兩張圖片。
// 未被選中的矢量圖片
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="32dp"
android:height="32dp"
android:viewportWidth="1024"
android:viewportHeight="1024">
<path
android:pathData="M120.47,177.69v662.59c0,30.12 24.09,60.24 60.24,60.24h662.59c30.12,0 60.24,-24.09 60.24,-60.24v-662.59c0,-30.12 -24.09,-60.24 -60.24,-60.24h-662.59c-36.14,0 -60.24,30.12 -60.24,60.24zM60.24,177.69c0,-66.26 54.21,-120.47 120.47,-120.47h662.59c66.26,0 120.47,54.21 120.47,120.47v662.59c0,66.26 -54.21,120.47 -120.47,120.47h-662.59c-66.26,0 -120.47,-54.21 -120.47,-120.47v-662.59z"
android:fillColor="#bfbfbf"/>
</vector>
//被選中的矢量圖片
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="32dp"
android:height="32dp"
android:viewportWidth="1024"
android:viewportHeight="1024">
<path
android:pathData="M849.32,334.31l-48.19,-48.19 -409.6,415.62 -168.66,-168.66 -48.19,42.16 216.85,216.85z"
android:fillColor="#13227a"/>
<path
android:pathData="M120.47,177.69v662.59c0,30.12 24.09,60.24 60.24,60.24h662.59c30.12,0 60.24,-24.09 60.24,-60.24v-662.59c0,-30.12 -24.09,-60.24 -60.24,-60.24h-662.59c-36.14,0 -60.24,30.12 -60.24,60.24zM60.24,177.69c0,-66.26 54.21,-120.47 120.47,-120.47h662.59c66.26,0 120.47,54.21 120.47,120.47v662.59c0,66.26 -54.21,120.47 -120.47,120.47h-662.59c-66.26,0 -120.47,-54.21 -120.47,-120.47v-662.59z"
android:fillColor="#13227a"/>
</vector>
屬性使用補充說明:
1. 負責複選框着色。兩屬性只在SDK>=21中生效,且會覆蓋checkMark="@drawable/selector_checked_textview"的效果
android:checkMarkTint="@android:color/black"
android:checkMarkTintMode="src_in"
xml文件修改後:
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightSmall"
android:checkMark="@drawable/selector_checked_textview"
android:gravity="center_vertical"
android:checkMarkTint="@android:color/black"
android:checkMarkTintMode="src_in"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/blue_trans" />
圖片,虛擬機API=21。
如上圖片我使用android:checkMarkTint="@android:color/black"和android:checkMarkTintMode="src_in"的效果將我使用checkMark屬性的效果給覆蓋了。
其他常用的方法:
1.是否選中。
public boolean isChecked ()
2.爲一個給定的Drawable設定檢查標記。當isChecked()爲true時則繪製
public void setCheckMarkDrawable (Drawable d)
3.爲一個給定的Drawable設定檢查標記,使用它的資源id來標識。當isChecked()爲true時則繪製
public void setCheckMarkDrawable (int resid)
4.改變文本視圖的選中狀態
public void setChecked (boolean checked)
5.反轉當前視圖的選中狀態
public void toggle ()
6.在單選模式下獲取被選中的item的索引index
listView.getCheckedItemPosition();
7.在複選模式下獲取item的選中情況Boolean集
listView.getCheckedItemPositions();
CheckedTextView使用上還是比Text和CheckBox的使用方便很多,還是推薦大家進行使用。