最初我想要一个复选标记,文本放置在复选标记的左侧。在这个网站上搜索后,我发现最好的解决方法是 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
主要用于在 ListView
的行中拥有单个 Checkable
View ,该 View 使用 控制其子项的可检查状态选择模式
。但是,由于 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."
/>
关于android: CheckedTextView 不能检查?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2627222/