android:CheckedTextView 无法检查?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2627222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:39:09  来源:igfitidea点击:

android: CheckedTextView cannot be checked?

androidcheckboxcheckedtextview

提问by Yang

Initially I wanted a checkmark where the text is placed on the left of the checkmark. After searching on this site I found out the best workaround is android:CheckedTextView? However, I found out that the checkmark cannot be changed manually by users. Is it by design?

最初我想要一个复选标记,其中文本位于复选标记的左侧。在这个网站上搜索后,我发现最好的解决方法是 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"/> 

采纳答案by Roman Nurik

You probably want to just use a regular CheckBox(which inherits from Buttonand thus TextView). CheckedTextViewis designed to work with list views. Example CheckBoxlayout XML is below:

您可能只想使用常规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" />

回答by Joe

It is possible, and somewhat simple to implement what you are looking for. Yes, CheckedTextViewis used primarily for having a single Checkableview in the row of a ListView, which controls its children's checkable states using choiceMode. However, since CheckBox does not appear to support a right-aligned checkbox on its own, and CheckedTextView isa right-aligned checkbox, it makes sense to want to use what's there.

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

Because ListView controls the checked state of a list item, the CheckedTextView itself does not respond to click events, and is not clickable or focusable by default. It does respond to pressed and focused states, however -- that means it can receive focus and click events, and looks correct as far as a checkbox should look. The only thing missing is that it does not toggle its checked state on click. Therefore, a quick OnClickListener that calls .toggle()will give you the end result you're looking for.

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

In summary, you need 3 things: clickable, focusable, and onClickListener:

总之,您需要 3 样东西:可点击、可聚焦和 onClickListener:

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

and layout file:

和布局文件:

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

回答by Nikolay Nikiforchuk

You can use and toggle CheckedTextView by the following way:

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

In layout:

在布局中:

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

In your activity:

在您的活动中:

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);
        }
}

And don't forget to put drawables. I get it from SDK ...\android-sdk-windows\platforms\android-10\data\res\drawable-mdpi\

并且不要忘记放置drawable。我从 SDK ...\android-sdk-windows\platforms\android-10\data\res\drawable-mdpi\

回答by vikoo

simple answer is use : isChecked()method instead of isSelected().

简单的答案是使用: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);            
        }
    }
});

and get the status using view.isChecked()method.

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

回答by Jason Robinson

This layout looks and behaves the same way as CheckedTextView:

此布局的外观和行为方式与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>

Only extra legwork is to set an OnClickListeneron the root view and call checkBox.toggle()on the CheckBox.

只有额外的跑腿是设置一个OnClickListener根视图和呼叫checkBox.toggle()CheckBox

回答by Geek4IT

Here is my use in SingleChoiceDialog

这是我在 SingleChoiceDialog 中的使用

1.select_dialog_singlechoice.xml

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

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

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

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

回答by Kent McNeill

If you want more fine-grained control over the label and the checkbox, another alternative is to use RelativeLayout and the android:layout_alignParentRight attribute:

如果您想对标签和复选框进行更细粒度的控制,另一种选择是使用 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>

you can then adjust the margin/etc of the textview and checkbox to suit your needs.

然后,您可以调整 textview 和复选框的边距/等以满足您的需要。