アンドロイド:CheckedTextViewを確認することができないのですか?
-
26-09-2019 - |
質問
当初、私はテキストはチェックマークの左側に配置されたチェックマークを望んでいました。 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 がの右揃えのチェックボックスであるため、しかし、それはそこに何を使用することは理にかなっています。
、CheckedTextView自体がイベントをクリックして応答しないと、デフォルトではクリック可能か、フォーカス可能ではありません。しかし、押され、集中状態に応答しない - それがフォーカスを受け取ると、イベントをクリックして、ルックスは限りチェックボックスがなりますよう修正することができることを意味していること。不足している唯一のものは、それはクリックでそのチェック状態を切り替えていないということです。そのため、迅速なOnClickListenerが呼び出しの.toggle()
は、あなたが探している最終的な結果を与えること。
要約すると、あなたは3つの事を必要とする
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 \データ\ RES \描画可能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>
もし、あなたのニーズに合わせてマージンの/ etcのTextViewとチェックボックスのを調整することができます。