CheckedTextView和AppCompatCheckedTextView

 

AppCompatCheckedTextView是继承自CheckedTextView的,在Androidx中进行支持。CheckedTextView可以看成是CheckBox、RadioGroup、TextView的组合使用,可以显示文字的同时进行单选或者复选。

当我们配合CheckedTextView使用ListView(RecyclerView不支持该方法的使用)时,使用setChoiceMode设置CHOICE_MODE_SINGLE(单选模式,只能选中一个选项)或者CHOICE_MODE_MULTIPLE时(复选模式,可以选中多个选项)。反正这控件还是很好用的,推荐大家使用。

废话不多说,贴上代码,兄弟你跑一下啥都知道了:

如上图片我使用android:checkMarkTint="@android:color/black"和android:checkMarkTintMode="src_in"的效果将我使用checkMark属性的效果给覆盖了。

class CheckTextViewFragment : Fragment(), View.OnClickListener {
    private var param1: String? = null
    private var param2: String? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    private lateinit var binding: FragmentCheckTextViewBinding
    private var mContext: Context? = null
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_check_text_view, container, false)
        binding.listener = this
        mContext = context
        initData()
        return binding.root
    }

    private fun initData() {
        val mData = ArrayList<String>()
        for (i in 0..4) {
            mData.add("item$i")
        }

        //通过listView设置模式  CHOICE_MODE_SINGLE(单选模式)
        binding.listView.choiceMode = AbsListView.CHOICE_MODE_MULTIPLE

        // 或者使用系统默认的布局 ,布局只决定显示样式,不决定是是单选还是复选模式
        // android.R.layout.simple_list_item_single_choice  显示的是圆的选择框
        // android.R.layout.simple_list_item_multiple_choice    显示的是方框的选择框
        if (context != null) {
            val mContext = context as Context
            val adapter = ArrayAdapter<String>(mContext, R.layout.item_checked_textview)
            adapter.addAll(mData)
            listView.adapter = adapter
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.checkbox2 -> {
                //反转当前视图的选中状态,选中变为不可选不可选变为可选
                binding.checkbox2.toggle()
                //在单选模式下获取被选中的item的索引index
                tvCheckCount.text = "选中的索引" + listView.checkedItemPosition
            }
            R.id.checkbox3 -> {
                binding.checkbox3.toggle()
                // 在复选模式下获取item的选中Boolean集
                tvCheckCount.text = "选中的数组" + listView.checkedItemPositions.size()
            }
            else -> println("未知类型无法判断")
        }
    }
}

 Fragment的布局文件:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="listener"
            type="android.view.View.OnClickListener" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">


        <CheckedTextView
            android:id="@+id/checkbox1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp"
            android:clickable="true"
            android:focusable="true"
            android:text="测试" />


        <!--  android:filterTouchesWhenObscured="true"//所在窗口被其它可见窗口遮住时,是否过滤触摸事件-->
        <!--  android:focusableInTouchMode="true"//定义是否可以通过touch获取到焦点   -->
        <CheckedTextView
            android:id="@+id/checkbox2"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="#999999"
            android:checkMark="?android:attr/listChoiceIndicatorMultiple"
            android:filterTouchesWhenObscured="true"
            android:focusableInTouchMode="true"
            android:gravity="center_vertical"
            android:onClick="@{listener}"
            android:paddingLeft="26dp"
            android:paddingRight="26dp"
            android:text="张三"
            android:textColor="@color/colorPrimary"
            android:textSize="16sp" />

        <CheckedTextView
            android:id="@+id/checkbox3"
            android:layout_width="match_parent"
            android:layout_height="48dp"
            android:background="#999999"
            android:checkMark="?android:attr/listChoiceIndicatorMultiple"
            android:filterTouchesWhenObscured="true"
            android:focusableInTouchMode="true"
            android:gravity="center_vertical"
            android:onClick="@{listener}"
            android:paddingLeft="26dp"
            android:paddingRight="26dp"
            android:text="张三"
            android:textColor="@color/blue"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/tvCheckCount"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <Button
            android:id="@+id/btnTest"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="@{listener}"
            android:text="广播发送信息" />

    </LinearLayout>

</layout>

 选项列表Item布局文件

<?xml version="1.0" encoding="utf-8"?>
<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:checkMarkTint="@android:color/black"
    android:checkMarkTintMode="src_in"
    android:filterTouchesWhenObscured="true"
    android:focusableInTouchMode="true"
    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"   文字的颜色-->

    <!--     android:focusableInTouchMode="true"//定义是否可以通过touch获取到焦点-->
    <!--     android:filterTouchesWhenObscured="true" 所在窗口被其它可见窗口遮住时,是否过滤触摸事件-->


    <!--    //负责复选框着色。会覆盖android:checkMark="@drawable/selector_checked_textview"的效果
        android:checkMarkTint="@android:color/black"
        android:checkMarkTintMode="src_in"-->

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>

其他常用的方法:

 

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)

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值