【问题标题】:android: CheckedTextView cannot be checked?android: CheckedTextView 不能检查?
【发布时间】:2011-02-07 07:20:40
【问题描述】:

最初我想要一个复选标记,文本放置在复选标记的左侧。在这个网站上搜索后,我发现最好的解决方法是 android:CheckedTextView?但是,我发现用户无法手动更改复选标记。是设计的吗?

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/autoupdatecheckboxview" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" 
 android:gravity="center_vertical" 
 android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
 android:paddingLeft="6dip" 
 android:paddingRight="6dip" 
 android:text="Pop up a message when new data available" 
 android:typeface="sans" android:textSize="16dip"/> 

【问题讨论】:

  • 那么CheckedTextView应该命名为ListCheckedTextView?该死的谷歌

标签: android checkbox checkedtextview


【解决方案1】:

您可能只想使用常规的CheckBox(继承自Button,因此继承自TextView)。 CheckedTextView 设计用于列表视图。示例CheckBox 布局 XML 如下:

<CheckBox
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Pop up a message when new data available"
    android:textSize="16dip" />

【讨论】:

  • 如果我不想添加其他视图怎么办?
  • 这并不能真正回答问题。考虑到人们知道 CheckBox 更合适,但仍然更喜欢使用 CheckedTextView。他们有兴趣知道如何让它发挥作用,而不是他们不应该。
【解决方案2】:

实现您正在寻找的东西是可能的,而且有点简单。是的,CheckedTextView 主要用于在ListView 的行中拥有单个Checkable 视图,该视图使用choiceMode 控制其子项的可检查状态。但是,由于 CheckBox 本身似乎不支持右对齐复选框,并且 CheckedTextView 右对齐复选框,因此想要使用那里的内容是有意义的。

由于 ListView 控制列表项的选中状态,所以 CheckedTextView 本身不响应点击事件,默认不可点击或聚焦。然而,它确实响应按下和聚焦状态——这意味着它可以接收焦点和单击事件,并且就复选框而言看起来是正确的。唯一缺少的是它不会在单击时切换其选中状态。因此,调用 .toggle() 的快速 OnClickListener 将为您提供所需的最终结果。

总而言之,你需要 3 个东西:可点击、可聚焦和 onClickListener:

    CheckedTextView chkBox = (CheckedTextView) findViewById(R.id.CheckedTextView01);
    chkBox.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v)
        {
            ((CheckedTextView) v).toggle();
        }
    });

和布局文件:

<CheckedTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/CheckedTextView01"
    android:checked="true"
    android:clickable="true"
    android:focusable="true"
    android:text="Label on Left Side of Checkbox."
    />

【讨论】:

  • 感谢您的解释:这并不明显,为什么这种控件默认情况下可能是不可点击的。
  • 很好的答案。要使 CheckedTextView 的行为更加类​​似于列表项,您应该将背景设置为 ​​?android:attr/listChoiceBackgroundIndicator
  • 真的很感谢@Joe,很好的解释
  • 在 Kotlin 中,我不得不使用“with (view as CheckedTextView) {isChecked = !isChecked}”或使用“if (checkbos.isChecked)... failed”,因为 isChecked 的值错误。
【解决方案3】:

如果您想对标签和复选框进行更细粒度的控制,另一种选择是使用 RelativeLayout 和 android:layout_alignParentRight 属性:

<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/my_checkbox_label" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="my checkbox label" />
    <CheckBox
        android:id="@+id/my_checkbox" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />
</RelativeLayout>

然后您可以调整文本视图和复选框的边距/等以满足您的需要。

【讨论】:

    【解决方案4】:

    您可以通过以下方式使用和切换 CheckedTextView:

    在布局中:

    <CheckedTextView
            android:id="@+id/cv_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Some text here" 
            android:textSize="18sp"
            android:gravity="center_vertical"
            android:clickable="true"
            android:checkMark="@drawable/btn_check_off"
            android:focusable="true"
            android:checked="false"
            android:onClick="toggle"/>
    

    在你的活动中:

    public void toggle(View v)
    {
        CheckedTextView cView = (CheckedTextView) v.findViewById(R.id.cv_file_name);
            if (cView.isSelected())
            {
                cView.setSelected(false);
                cView.setCheckMarkDrawable (R.drawable.btn_check_off);
            }
            else
            {
                cView.setSelected(true);
                cView.setCheckMarkDrawable (R.drawable.btn_check_on);
            }
    }
    

    别忘了放drawables。我从 SDK ...\android-sdk-windows\platforms\android-10\data\res\drawable-mdpi\

    【讨论】:

      【解决方案5】:

      此布局的外观和行为与CheckedTextView 相同:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="?attr/dropdownListPreferredItemHeight"
          android:gravity="center_vertical" >
      
          <TextView
              android:id="@android:id/text1"
              style="?android:attr/spinnerDropDownItemStyle"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_weight="1"
              android:ellipsize="marquee"
              android:singleLine="true" />
      
          <CheckBox
              android:id="@android:id/checkbox"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:clickable="false"
              android:duplicateParentState="true"
              android:focusable="false" />
      
      </LinearLayout>
      

      唯一额外的工作是在根视图上设置OnClickListener 并在CheckBox 上调用checkBox.toggle()

      【讨论】:

        【解决方案6】:

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

        【讨论】:

        • 变量 mCheckedItem 在哪里,它在我的 ArrayAdapter 中没有。
        • 你可以把你的 CheckedTextView 放在你想要的任何地方。
        【解决方案7】:

        简单的答案是添加您自己的 onClickListener,并使用:isChecked() 方法而不是 isSelected()

        CheckedTextView chkBox = (CheckedTextView) findViewById(R.id.CheckedTextView01);
        chkBox.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                if(((CheckedTextView) v).isChecked()){
                    ((CheckedTextView) v).setChecked(false);
                }else{
                    ((CheckedTextView) v).setChecked(true);            
                }
            }
        });
        

        并使用view.isChecked() 方法获取状态。

        【讨论】:

          猜你喜欢
          • 2023-03-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多