Android 如何使用checkedtextview

发布于 2024-11-09 05:00:54 字数 2367 浏览 2 评论 0原文

标题不言而喻。我已经阅读了几篇文章、主题,但仍然没有弄清楚如何使用checkedtextview。我想要一个带有可检查项目的列表视图。 在下面的代码中,我使用列表视图并用字符串数组填充它。但是如何将其更改为checkedtextview呢?

删除.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>

删除.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();

}
}

the title speaks for itself. I am over several articles, topics and still haven't figured out how to use checkedtextview. I want a listview with checkable items.
In the following code i am using a listview and populating it with a string array. But how to change it to 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();

}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

朕就是辣么酷 2024-11-16 05:00:54

在您的 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>

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>
猫弦 2024-11-16 05:00:54

您可以使用 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"
    />

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

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.

东走西顾 2024-11-16 05:00:54

这是在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"/>

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"/>
╭ゆ眷念 2024-11-16 05:00:54

这是我在 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 的 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);

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);
就此别过 2024-11-16 05:00:54

您可以在适配器布局中编写具有复选框和文本视图的自定义适配器。然后在list的setOnItemClick监听器中处理多选和单选。

有关更多信息,您可以检查此链接 如何在不使用选择器的情况下将 ListView 所选项目颜色从默认更改为红色

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文