簡體   English   中英

當用戶標記列表項內的復選框時啟用按鈕

[英]Enable a button when user mark a checkbox inside a list item

我正在開發一個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>

這是帶有list的活動布局:

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

為您的復選框實現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.

            }
        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM