2012-04-16 7 views
0

私はAndroid 3.1アプリケーションを開発しています。ユーザーがリスト項目のチェックボックスをマークすると、ボタンを有効にします

私はこのレイアウトでアイテムをListViewあります

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <CheckBox 
     android:id="@+id/itemCheckBox" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</LinearLayout> 

をし、これがリストとアクティビティのレイアウトです:

ユーザーが1つまたは複数のリスト項目を選択したときに downloadFormsButtonを有効にするにはどうすればよい
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 
    <ListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="629dp" > 
    </ListView> 
    <Button 
     android:id="@+id/downloadFormsButton" 
     android:enabled="false" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="10dp" 
     android:text="@string/download_forms_button" /> 
    <TextView 
     android:id="@+id/formErrorMsg" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="10dp" 
     android:textSize="16sp" > 
    </TextView> 
</LinearLayout> 

'チェックボックス?

答えて

1

チェックボックスにonCheckedChangeListenerを実装し、アダプタ内のチェックされた項目のリストを管理します。チェックされた状態が変更されるたびに、チェックされた項目のリストが空であるかどうかを確認し、それに基づいてボタンを有効または無効にします。

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

      @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
      { 
       if (isChecked) 
        checkedItems.add(itemId); 
       else 
        checkedItems.remove(itemId); 

       downloadFormsButton.setEnabled(checkedItems.size() > 0); //sets button enabled = true if size is greater than 0. 

      } 
     }); 
関連する問題