2014-09-05 57 views
0

我有一個CheckBox可啓用/禁用一對EditText視圖。首先,當活動開始時,我可以點擊EditText,但是當我按下CheckBox(將啓用設置爲false),然後再次按下複選框(據說設置EditText啓用)時,我可以' t點擊EditText。禁用並重新啓用時無法點擊EditText

這是java代碼

@Override 
public void onCheckedChanged(CompoundButton buttonView, 
     boolean isChecked) { 
    final EditText etPersonTitle = (EditText)v.findViewById(R.id.et_person_title); 
    final EditText etFirstname = (EditText)v.findViewById(R.id.et_first_name); 
    final EditText etSurname = (EditText)v.findViewById(R.id.et_surname); 

    if(isChecked){ 
     etPersonTitle.setText(R.string.unknown); 
     etFirstname.setText(R.string.unknown); 
     etSurname.setText(R.string.unknown); 
    } 

    else { 
     etPersonTitle.setText(""); 
     etFirstname.setText(""); 
     etSurname.setText(""); 
    } 

    etPersonTitle.setEnabled(!isChecked); 
    etPersonTitle.setFocusable(!isChecked); 
    etFirstname.setEnabled(!isChecked); 
    etFirstname.setFocusable(!isChecked); 
    etSurname.setEnabled(!isChecked); 
    etSurname.setFocusable(!isChecked); 
} 

這裏是XML:

<TableRow 
       android:id="@+id/tableRow1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="right" 
        android:text="@string/title" /> 

       <EditText 
        android:id="@+id/et_person_title" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:ems="15" 
        android:inputType="textPersonName" 
        android:textAppearance="?android:attr/textAppearanceSmall" > 
       </EditText> 
      </TableRow> 

      <TableRow 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="right" 
        android:text="@string/first_name" /> 

       <EditText 
        android:id="@+id/et_first_name" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:ems="15" 
        android:inputType="textPersonName" 
        android:textAppearance="?android:attr/textAppearanceSmall" > 
       </EditText> 
      </TableRow> 

      <TableRow 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="right" 
        android:text="@string/surname" /> 

       <EditText 
        android:id="@+id/et_surname" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:ems="15" 
        android:inputType="textPersonName" 
        android:textAppearance="?android:attr/textAppearanceSmall" > 
       </EditText> 
      </TableRow> 

      <LinearLayout 
       android:id="@+id/ll_unknown_customer" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:orientation="horizontal" > 

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="10dp" 
        android:text="@string/unknown_customer" /> 

       <CheckBox 
        android:id="@+id/cb_unknown_customer" 
        style="@style/checkbox_style" 
        android:background="@drawable/checkbox_background" 
        android:button="@null" 
        android:checked="false" /> 

      </LinearLayout> 

有一點要注意的是,TableLayout是滾動型內。我不確定這是什麼原因導致這個問題,但這是我的第一個懷疑。

我試圖setClickable(true),但它也不起作用。

在此先感謝。

+0

你可以嘗試顛倒你的邏輯,以便它變成setEnabled(isChecked);和三個EditTexts的setFocusable(isChecked)?如果問題保持不變,請分享結果。 – helleye 2014-09-05 14:24:42

+0

首先大多數編碼器不使用負邏輯!!!!!而且絕對沒有人喜歡在某個問題上閱讀,因爲這意味着懷疑你甚至沒有嘗試過。 – danny117 2014-09-05 14:27:52

+0

因此,您使用TableLayout提供的XML是單個ListView項目的佈局?當使用ListView的自定義ArrayAdapter時,遇到['getView回收原則'](http://stackoverflow.com/a/14108676/1682559)列表對於屏幕來說很大(因此ListView變爲滾動)。這個問題很可能存在於ArrayAdapter的getView方法中。從提供的代碼中,我無法真正發現問題,但由於過去我在getView回收器方面遇到了一些問題,我認爲這也會導致一些問題。 – 2014-09-05 14:56:19

回答

0

這樣做。

if (isChecked) { 
       etPersonTitle.setText("asd"); 
       etFirstname.setText("asd"); 
       etSurname.setText("asd"); 
       etPersonTitle.setEnabled(!isChecked); 
       etPersonTitle.setFocusable(!isChecked); 
       etFirstname.setEnabled(!isChecked); 
       etFirstname.setFocusable(!isChecked); 
       etSurname.setEnabled(!isChecked); 
       etSurname.setFocusable(!isChecked); 
      } 

      else { 
       etPersonTitle.setText(""); 
       etFirstname.setText(""); 
       etSurname.setText(""); 

       etPersonTitle.setEnabled(true); 
       etPersonTitle.setFocusableInTouchMode(true); 

       etFirstname.setEnabled(true); 
       etFirstname.setFocusableInTouchMode(true); 

       etSurname.setEnabled(true); 
       etSurname.setFocusableInTouchMode(true); 

      } 
+0

這是我正在尋找的答案。我可以問爲什麼我必須setFocusableInTouchMode而不是setFocusable? – 2014-09-05 15:25:32