Android how to use checkedtextview

江枫思渺然 提交于 2019-12-03 05:49:17
star angel

In your xml file you're using textview as list item. Instead of that use CheckedTextView. You can directly drag and drop that from your widgets palette. You can use the CheckedTextView as both TextView and CheckBox. Below is an example if how to use it

   <CheckedTextView 
      xmlns:android="http://schemas.android.com/apk/res/android"     
      style="@style/NormalText"  
      android:id="@+id/checkList" 
      android:paddingLeft="20dip" 
      android:paddingRight="20dip" 
      android:paddingTop="10dip"
      android:paddingBottom="10dip" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="?android:attr/listPreferredItemHeight"  
      android:gravity="center_vertical"  
      android:checkMark="@drawable/our_checkbox"></CheckedTextView>

You can use android:drawableLeft to set the checkbox on left. You can use the code snippet below for achieving it.

<CheckedTextView
    android:id="@+id/checkedTextView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/custom_radio_check"
    android:gravity="center_vertical"
    android:textSize="12sp"
    android:textColor="#3d484d"
    android:paddingLeft="14dp"
    android:paddingTop="12dp"
    android:paddingBottom="12dp"
    />

This will move your selector to left side of text. You can adjust the rest of the parameters like padding, gravity, etc.

This is the code to use the default CheckdTextView in android

<CheckedTextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:checkMark="?android:attr/textCheckMark"
                android:paddingLeft="6dip"
                android:paddingRight="6dip"
                android:checked="true"
                android:gravity="center_vertical"
                android:text="- Action finished"/>

Here is my use in SingleChoiceDialog

1.select_dialog_singlechoice.xml

<?xml version="1.0" encoding="UTF-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="@style/PopupSelectList"
    android:checkMark="@drawable/radio"
    android:ellipsize="marquee"
    android:gravity="center_vertical"
    android:paddingLeft="12.0dip"
    android:paddingRight="10.0dip" />

2.style.xml

<style name="PopupSelectList">
    <item name="android:textSize">16.0sp</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:background">@drawable/list_item_selector</item>
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:minHeight">@dimen/dialog_select_height</item>
</style>

3.ratio.xml

<?xml version="1.0" encoding="UTF-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/dot_selected" android:state_checked="true" android:state_window_focused="false"/>
    <item android:drawable="@drawable/dot_normal" android:state_checked="false" android:state_window_focused="false"/>
    <item android:drawable="@drawable/dot_normal" android:state_checked="false"/>
    <item android:drawable="@drawable/dot_selected" android:state_checked="true"/>
</selector>

4.In Adapter's getView

CheckedTextView title = (CheckedTextView) convertView
            .findViewById(android.R.id.text1);
    title.setText(mItems[position]);
    title.setSelected(mCheckedItem == position ? true : false);
                title.setCheckMarkDrawable(position == mCheckedItem ?                   R.drawable.dot_selected
            : R.drawable.dot_normal);
Supriya

You can write Custom adapter having CheckBox and textview in adapter layout. Then handle in list's setOnItemClick listener for multiple selection and single selection.

for more you can check this link How to change ListView Selected items color from default to red without using selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!