Android CheckedTextView 实现单选与多选
生活随笔
收集整理的這篇文章主要介紹了
Android CheckedTextView 实现单选与多选
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
以下demo 地址
單選與雙選實現(xiàn)的效果如下
CheckedTextView 繼承的是 TextView 實現(xiàn) Checkable 具有TextView 的屬性
一般使用結(jié)合ListView?
xml 屬性
android:checkMark : 可繪制用于復(fù)選標記圖形
android:checked : 指示文本的初始檢查狀態(tài)
默認提供單選框樣式:
android:checkMark="?android:attr/listChoiceIndicatorSingle"
默認的效果圖如下
默認提供復(fù)選框樣式:
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
默認的效果圖如下
下面結(jié)合代碼說下使用
MainActivity 的代碼如下
public class MainActivity extends AppCompatActivity {private String[] data = {"Android", "Java", "C++", "PHP", "Python", "JS", "HTML", "CSS"};private ListView listView;ArrayList<String> arrayList = new ArrayList<>();SingleCheckAdapter adapter;private ListView listView1;ArrayList<String> arrayList1 = new ArrayList<>();List<String> list = new ArrayList<>();MultCheckAdapter adapter1;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 單選數(shù)據(jù)listView = findViewById(R.id.listView);arrayList.addAll(Arrays.asList(data));adapter = new SingleCheckAdapter(MainActivity.this, arrayList);listView.setAdapter(adapter);listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {Log.v("----hxm", "您選擇的是:" + arrayList.get(position));}});//多選數(shù)據(jù)listView1 = findViewById(R.id.listView1);arrayList1.addAll(Arrays.asList(data));adapter1 = new MultCheckAdapter(MainActivity.this, arrayList1);listView1.setAdapter(adapter1);listView1.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view, int position, long id) {if (list.contains(arrayList1.get(position))) {list.remove(arrayList1.get(position));} else {list.add(arrayList.get(position));}Log.v("----hxm", "您選擇的是:" + list);}});}}
SingleCheckAdapter 的代碼如下:
package com.hly.timeactivity;import java.util.ArrayList;import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckedTextView;public class SingleCheckAdapter extends BaseAdapter {private ArrayList<String> list;private Context mContext;private ArrayList<String> resultList = new ArrayList<>();public SingleCheckAdapter(Context context, ArrayList<String> list) {this.list = list;this.mContext = context;}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return list.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {final ViewHolder holder;if (convertView == null) {holder = new ViewHolder();convertView = LayoutInflater.from(mContext).inflate(R.layout.single_list_item_layout, null);holder.checkedTextView = convertView.findViewById(R.id.checkedTextView);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}holder.checkedTextView.setText(list.get(position));return convertView;}public static class ViewHolder {CheckedTextView checkedTextView;}}
xml :single_list_item_layout
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="60dp"android:id="@+id/checkedTextView"android:checkMark="@drawable/drawable_single_checked_view"android:text="CheckedTextView"android:textSize="25sp" />
drawable_single_checked_view
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@drawable/single_select" android:state_checked="true" /><item android:drawable="@drawable/single_unselect" android:state_checked="false" />
</selector>
由于多選和單選差不多,這里就不貼代碼了, 代碼都很簡單的,別的就不多說了, 不過還需要特別注意下,list 的item 最外層的布局要使用CheckedTextView, 這個監(jiān)聽的時候點擊item 就相當于點擊了CheckedTextView,
如果最外層不是CheckedTextView 點擊事件會有問題.
總結(jié)
以上是生活随笔為你收集整理的Android CheckedTextView 实现单选与多选的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ArrayAdapter require
- 下一篇: 喝白葡萄酒有哪些好处?红葡萄酒和白葡萄酒