首先,我想在文本被放置在复选标记的左侧复选标记。在这个网站搜索后,我找到了最好的解决方法就是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"/> 
有帮助吗?

解决方案

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

其他提示

这是可能的,有些简单的实现你在找什么。是的,CheckedTextView主要用于具有Checkable,使用ListView控制其子女可检查状态的排在一个choiceMode视图。然而,由于复选框似乎并不支持它自己的一个右对齐复选框,CheckedTextView的的右对齐复选框,它是有道理的要使用那里的东西。

由于的ListView控件列表项的选中状态时,CheckedTextView本身不响应点击事件,并默认情况下不点击或可成为焦点。它响应于按下并集中状态,但是 - 这意味着它可以接收焦点和单击事件,看起来就一个复选框,应该纠正。唯一缺少的就是它没有做切换上点击它选中状态。因此,快速OnClickListener的呼叫.toggle()会给你你正在寻找的最终结果。

总之,则需要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:

在布局:

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

不要忘了把绘项目。我把它从SDK \ Android的SDK-WINDOWS \平台\ Android的10 \ DATA \水库\绘制,MDPI \

简单的答案是使用: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()方法

和得到的状态。

此布局的外观和行为相同的方式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.toggle()CheckBox

下面是我在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适配器的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);

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

您可以再调整的TextView和复选框的保证金/等,以满足您的需求。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top