2010-04-13 26 views
38

最初我想要一個複選標記,其中的文本位於複選標記的左側。在這個網站上搜索後,我發現最好的解決方法是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"/> 
+0

然後CheckedTextView應該被命名爲ListCheckedTextView?該死的谷歌 – 2016-08-27 23:24:41

回答

29

你可能只想使用常規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" /> 
119

這是可能的,有些簡單的實現你在找什麼。是的,CheckedTextView主要用於在ListView的行中使用單個Checkable視圖,該視圖使用choiceMode控制其子可檢查狀態。但是,由於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." 
    /> 
+0

感謝您的解釋:這並不明顯,爲什麼這種控制可能是默認情況下不可點擊。 – Smileek 2014-03-04 11:45:44

+4

很好的答案。爲了使CheckedTextView的行爲更類似於列表項,您應該將背景設置爲?android:attr/listChoiceBackgroundIndicator。 – Moritz 2014-04-18 20:08:10

+0

_ + 1_爲此以及http://stackoverflow.com/questions/12641529/unable-to-check-uncheck-checkedtextview-inside-getview – nAkhmedov 2015-07-08 10:55:06

0

如果你想在標籤和更細粒度的控制複選框,另一種方法是使用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和複選框的邊距/等以滿足您的需求。

9

您可以使用,並通過以下方式切換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 \

+0

謝謝,這對我很好。 – Jeff 2013-02-11 16:51:41

1

這種佈局的外觀和行爲方式一樣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上調用checkBox.toggle()

1

這是我在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.在Adapter的g etView

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); 
+0

其中是變量mCheckedItem它的否在我的ArrayAdapter。 – JPM 2013-10-30 17:42:54

+0

你可以把你的CheckedTextView放在你想要的任何地方。 – Geek4IT 2013-11-02 08:40:21

1

簡單的答案是用:isChecked()方法代替​​。

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()方法獲取狀態。