启用按钮时用户标记列表项内的复选框 [英] Enable a button when user mark a checkbox inside a list item

查看:97
本文介绍了启用按钮时用户标记列表项内的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android 3.1应用程序。

I'm developing an Android 3.1 application.

我有一个的ListView 与此布局的项目:

I have a ListView with items with this layout:

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

推荐答案

实现你的一个复选框,并onCheckedChangeListener管理适配器检查的项目清单。每当选中状态被改变的检查,看看您的托运物品的列表为空,启用/禁用基于您的按钮。

Implement an onCheckedChangeListener for your checkbox and manage a list of checked items in your adapter . Whenever the checked state is changed check to see if your list of checked items is empty and enable/disable your button based on that.

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.

            }
        });

这篇关于启用按钮时用户标记列表项内的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆