Yan*_*ang 47 checkbox android checkedtextview
最初我想要一个复选标记,其中文本位于复选标记的左侧.在这个网站上搜索后,我发现最好的解决方法是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"/>
Run Code Online (Sandbox Code Playgroud)
Joe*_*Joe 122
实现您正在寻找的东西是可能的,而且有点简单.是的,CheckedTextView主要用于在a Checkable的行中具有单个视图,该视图ListView使用控制其子级的可检查状态choiceMode.但是,由于CheckBox似乎不支持右对齐的复选框,而CheckedTextView 是一个右对齐的复选框,因此想要使用那里的内容是有意义的.
由于ListView控制列表项的已检查状态,因此CheckedTextView本身不响应单击事件,并且默认情况下不可单击或可聚焦.但它确实响应了按下和聚焦的状态 - 这意味着它可以接收焦点和点击事件,并且就复选框看起来看起来是正确的.唯一缺少的是它不会在点击时切换其检查状态.因此,调用的快速OnClickListener .toggle()将为您提供所需的最终结果.
总之,您需要3件事:clickable,focusable和onClickListener:
CheckedTextView chkBox = (CheckedTextView) findViewById(R.id.CheckedTextView01);
chkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
((CheckedTextView) v).toggle();
}
});
Run Code Online (Sandbox Code Playgroud)
和布局文件:
<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."
/>
Run Code Online (Sandbox Code Playgroud)
Rom*_*rik 38
你可能想只使用常规的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" />
Run Code Online (Sandbox Code Playgroud)
您可以通过以下方式使用和切换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"/>
Run Code Online (Sandbox Code Playgroud)
在您的活动中:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
并且不要忘记放置drawables.我从SDK ...\android-sdk-windows\platforms\android-10\data\res\drawable-mdpi \得到它
| 归档时间: |
|
| 查看次数: |
47916 次 |
| 最近记录: |