CheckedTextView是什么
CheckedTextView繼承自TextView且實作了Checkable介面,對TextView界面和顯示進行了擴展的控制元件,支持Checkable,可以實作單選或多選功能,在你懶得使用兩者結合的時候,這就是不二選擇,
主要XML屬性
android:checkMark 按鈕樣式,
-
默認單選框樣式:android:checkMark="?android:attr/listChoiceIndicatorSingle"
-
默認復選框樣式:android:checkMark="?android:attr/listChoiceIndicatorMultiple"
-
當然也可以使用drawable自定義樣式
android:checkMarkTint 按鈕的顏色,
android:checkMarkTintMode 混合模式按鈕的顏色,
android:checked 初始選中狀態,默認false,
在點擊事件里判斷狀態設定狀態
CheckedTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckedTextView.toggle();//切換選中與非選中狀態
}
});
咱們看看CheckedTextView.toggle()是干嘛的
public void toggle() {
setChecked(!mChecked);
}
就是實作這個控制元件的狀態反操作,
第一次點擊無效
android:focusableInTouchMode="true",這個屬性加上會導致第一次點擊觸發不了選擇事件,
實體
官方檔案指出,結合ListView使用更佳,咱下面通過一個栗子了解一下,下面是效果圖:

1.主界面CheckedTextViewActivity.java
public class CheckedTextViewActivity extends AppCompatActivity {
private ListView lv_ctv_multiple,lv_ctv_single;
private CtvMultipleAdapter ctvAdapter;
private TextView tv_multiple_title,tv_single_title;
private CtvSingleAdapter ctvSingleAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_textview_ctv);//加載布局檔案
initView();
}
private void initView() {
ArrayList<String> ctvString = new ArrayList<>();
ctvString.add("秦始皇嬴政");
ctvString.add("漢高祖劉邦");
ctvString.add("唐太宗李世民");
ctvString.add("宋太祖趙匡胤");
//復選
lv_ctv_multiple = findViewById(R.id.lv_ctv_multiple);
tv_multiple_title = findViewById(R.id.tv_multiple_title);
ctvAdapter = new CtvMultipleAdapter(this,ctvString,tv_multiple_title);
lv_ctv_multiple.setAdapter(ctvAdapter);
//設定Item間距
lv_ctv_multiple.setDividerHeight(0);
//單選
lv_ctv_single = findViewById(R.id.lv_ctv_single);
tv_single_title = findViewById(R.id.tv_single_title);
ctvSingleAdapter = new CtvSingleAdapter(this,ctvString,tv_single_title);
lv_ctv_single.setAdapter(ctvSingleAdapter);
//設定Item間距
lv_ctv_single.setDividerHeight(0);
}
}
2.主布局activity_textview_ctv.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/dimen_20">
<TextView
android:id="@+id/tv_multiple_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dimen_10"
android:textColor="@color/black"
android:text="復選"
android:textSize="@dimen/text_size_16" />
<ListView
android:id="@+id/lv_ctv_multiple"
android:layout_width="match_parent"
android:layout_height="180dp" />
<TextView
android:id="@+id/tv_single_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/dimen_10"
android:text="單選"
android:textColor="@color/color_188FFF"
android:layout_marginTop="@dimen/dimen_10"
android:textSize="@dimen/text_size_20" />
<ListView
android:id="@+id/lv_ctv_single"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
3.復選框Adapter
public class CtvMultipleAdapter extends BaseAdapter {
private LayoutInflater mInflater;//得到一個LayoutInfalter物件用來匯入布局
private List<String> list;
private TextView tvTitle;
private List<String> selectList = new ArrayList<>();
public CtvMultipleAdapter(Context context, List<String> list, TextView tv) {
this.mInflater = LayoutInflater.from(context);
this.list = list;
tvTitle = tv;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final CtvViewHolder holder;
final String string = list.get(position);
//觀察convertView隨ListView滾動情況
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_ctv_multiple, null);
holder = new CtvViewHolder();
/*得到各個控制元件的物件*/
holder.ctv_top = (CheckedTextView) convertView.findViewById(R.id.ctv_top);
convertView.setTag(holder);//系結ViewHolder物件
} else {
holder = (CtvViewHolder) convertView.getTag();//取出ViewHolder物件
}
holder.ctv_top.setText(string);
//默認選中狀態
if(holder.ctv_top.isChecked()){
//list未包含選中string;
if(!selectList.contains(string)){
selectList.add(string);
}
}
if (selectList.size() == 0) {
tvTitle.setText("");
} else {
tvTitle.setText(selectList.toString());
}
holder.ctv_top.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ctv_top.toggle();//切換選中與非選中狀態
//單選
if(holder.ctv_top.isChecked()){//
//list未包含選中string;
if(!selectList.contains(string)){
selectList.add(string);
}
}else{
//list未包含選中string;
if(selectList.contains(string)){
selectList.remove(string);
}
}
if (selectList.size() == 0) {
tvTitle.setText("");
} else {
tvTitle.setText(selectList.toString());
}
}
});
return convertView;
}
/*存放控制元件*/
public class CtvViewHolder {
public CheckedTextView ctv_top;
}
@Override
public boolean areAllItemsEnabled() {
return false;//Item不可點擊
}
@Override
public boolean isEnabled(int position) {
return false;//Item不可點擊
// 攔截事件交給上一級處理
//return super.isEnabled(position);
}
}
4.復選框adapter對應布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<CheckedTextView
android:id="@+id/ctv_top"
android:checked="true"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:checkMarkTint="@color/color_FF773D"
android:padding="10dp"
android:textSize="16sp"
android:layout_marginTop="3dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
這里用到checkMark(默認復選框樣式)、checkMarkTint(復選框顏色設為黃色)、和checked(true默認選中)幾個屬性,可以更好的理解他們,
5.單選框adapter
private String selectStr="";//全域變數
holder.ctv_top.setText(string);
holder.ctv_top.setChecked(selectStr.equals(string));
holder.ctv_top.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.ctv_top.toggle();//切換選中與非選中狀態
//單選
if(holder.ctv_top.isChecked()){
selectStr=string;
}else{
selectStr="";
}
tvTitle.setText(selectStr);
notifyDataSetChanged();
}
});
大部分與復選框CtvMultipleAdapter設定相同,區域分不同就不做多重復了,
6.單選框adapter對應布局
<CheckedTextView
android:id="@+id/ctv_top"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:padding="10dp"
android:textSize="16sp"
android:layout_marginTop="3dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
僅使用單選默認樣式,
7.邏輯處理從adapter放在主界面處理
ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//在這里進行單選復選的邏輯處理
}
});
使用CheckedTextView配合ListView實作單選與多選的功能我們實作了,到這里,關于CheckedTextView我們也就介紹完了,嘿嘿,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/289635.html
標籤:其他
