Android: CheckedTextView ne peut pas être vérifié?
-
26-09-2019 - |
Question
Dans un premier temps, je voulais un crochet où le texte est placé à gauche de la coche. Après une recherche sur ce site que je trouve la meilleure solution est android: CheckedTextView? Cependant, je trouve que la case ne peut pas être modifié manuellement par les utilisateurs. Est-ce par la conception?
<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"/>
La solution
Vous voulez probablement simplement utiliser régulièrement CheckBox
(héritant de Button
et donc TextView
). CheckedTextView
est conçu pour fonctionner avec une vue sur la liste. Exemple CheckBox
mise en page XML est ci-dessous:
<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" />
Autres conseils
Il est possible, et assez simple à mettre en œuvre ce que vous recherchez. Oui, CheckedTextView
est utilisé principalement pour avoir une seule vue Checkable
dans la ligne d'un ListView
, qui contrôle les états cochables de ses enfants en utilisant choiceMode
. Toutefois, étant donné CheckBox ne semble pas soutenir une case à cocher aligné à droite sur elle-même, et CheckedTextView une case à cocher aligné à droite, il est logique de vouloir utiliser ce qui existe.
Parce que ListView contrôle l'état coché d'un élément de la liste, le CheckedTextView lui-même ne répond pas à cliquer sur les événements, et ne sont pas cliquables ou focalisable par défaut. Il ne répond aux états pressés et ciblés, mais - ce qui signifie qu'il peut recevoir le focus et cliquez sur les événements, et semble correcte jusqu'à une case à cocher doit regarder. La seule chose qui manque est qu'il ne bascule son état activé en cliquant dessus. Par conséquent, un OnClickListener rapide que les appels .toggle()
vous donnera le résultat final que vous cherchez.
En résumé, vous avez besoin de 3 choses: cliquables, vario et onClickListener:
CheckedTextView chkBox = (CheckedTextView) findViewById(R.id.CheckedTextView01);
chkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
((CheckedTextView) v).toggle();
}
});
et le fichier de mise en page:
<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."
/>
Vous pouvez utiliser et bascule CheckedTextView par la manière suivante:
Dans la mise en page:
<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"/>
Dans votre activité:
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);
}
}
Et ne pas oublier de mettre dessinables. Je l'obtiens du SDK ... \ android-sdk-windows \ plateformes \ android-10 \ data \ res \ drawable-mdpi \
réponse est simple d'utilisation:. Méthode isChecked()
au lieu de 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);
}
}
});
et obtenir le statut en utilisant la méthode view.isChecked()
.
Cette disposition ressemble et se comporte de la même façon que 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>
Seul travail sur le terrain supplémentaire est de mettre un OnClickListener
sur la vue racine et checkBox.toggle()
d'appel sur le CheckBox
.
Voici mon utilisation dans 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>
getView de 4.In Adaptateur
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);
Si vous voulez plus de contrôle de fin sur l'étiquette et la case à cocher, une autre alternative est d'utiliser RelativeLayout et l'attribut 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>
vous pouvez alors régler la marge / etc de la textview et case à cocher en fonction de vos besoins.