Android如何使用CheckedTextView

14

题目已经说明了。我看过很多文章和主题,但仍然不知道如何使用CheckedTextView。我想要一个带有可选项的ListView。 在下面的代码中,我使用了一个ListView,并用一个字符串数组填充它。但如何将其改为CheckedTextView?

delete.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:paddingTop="4dip"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:id="@+id/linlay0"
     android:background="@color/list_bg">
  <TextView 
        android:id="@+id/TextView00"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#D8D8D8"
        android:textColor="#424242"
        android:gravity="center_horizontal|center_vertical"
        android:textSize="20px"
        android:height="40px"
        android:textStyle="bold"
        />
     <ListView android:id="@+id/ListView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

delete_lv.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<TextView
android:id="@+id/list_content"
android:textColor="#222222"
android:gravity="center"
android:text="sample"
android:layout_margin="4dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>

</LinearLayout>

Delete.java:

public class Delete extends Activity {

    ListView lv1;
    ArrayAdapter<String> adapter1;
    private String lv_items[] = { "Android", "iPhone", "BlackBerry",
                 "AndroidPeople", "J2ME", "Listview", "ArrayAdapter", "ListItem",
                 "Us", "UK", "India" };

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.delete);

TextView tv0 = (TextView)findViewById(R.id.TextView00);
tv0.setText("Deletes");

lv1 = (ListView) findViewById(R.id.ListView01);
adapter1 = new ArrayAdapter<String>(Delete.this,R.layout.list_black_text,R.id.list_content, lv_items);
lv1.setAdapter(adapter1);
adapter1.notifyDataSetChanged();

}
}
5个回答

17

你的xml文件中使用了textview作为列表项。相反,使用CheckedTextView。您可以直接从小部件调色板中拖放它。您可以将CheckedTextView用作TextViewCheckBox。以下是如何使用它的示例:

   <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>

2
有没有想法如何将复选框放在文本的左侧? - Siddharth
注意:our_checkbox 必须是一个带有定义的 android:state_checked="true" 状态的选择器。 - artkoenig
3
如果你只需要默认的Android复选框外观,请使用android:checkMark="?android:attr/listChoiceIndicatorMultiple"。 - Filipe Silva
有一个名为fontFamily的属性,我可以在其中使用自定义字体吗?在官方API文档页面上找不到它。 - Talha
如果您正在使用多选功能,则可能希望改用android:checkMark="?android:attr/listChoiceIndicatorMultiple" - ccpizza

4

这是在Android中使用默认的CheckdTextView的代码

<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"/>

4
您可以使用android:drawableLeft将复选框设置在左侧。您可以使用下面的代码片段来实现它。
<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"
    />

这将把您的选择器移动到文本的左侧。您可以调整其余参数,如填充、重力等。


4
但是,这正是 CheckBox 所做的。 - Warpzit

2

这里是我在 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. 在适配器的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);

-1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接