2012-04-16 20 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> 

这是活动与名单布局:

<?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> 

如何启用downloadFormsButton当用户选择一个或多个列表项'复选框?

回答

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. 

      } 
     }); 
相关问题