Android 使用CheckBox实现多选效果

CheckBox:复选框
1.有两种状态:
 选中状态(true),未选中状态(false)
2.属性:
 android:id="@+id/checkbox"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:checked="false"
 android:text="男"

CheckBox的默认android:checked属性为false。
checkBox的OnCheckedChangeListener事件检查勾是否勾选。
样例程序中有3个CheckBox和1个TextView,TextView事实演示了有多少CheckBox被勾选了以及被勾选的CheckBox的名称。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="篮球" />

    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="足球" />

    <CheckBox
        android:id="@+id/checkBox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="乒乓球" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0项选择" />

</LinearLayout>
activity_main.xml
package com.example.checkbox;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
    
    private CheckBox basketballCheckBox;
    private CheckBox footballCheckBox;
    private CheckBox pingpongCheckBox;

    private boolean[] checkedArray = new boolean[] {false, false, false}; 
    
    private TextView textView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        basketballCheckBox = (CheckBox) findViewById(R.id.checkBox1);
        footballCheckBox = (CheckBox) findViewById(R.id.checkBox2);
        pingpongCheckBox = (CheckBox) findViewById(R.id.checkBox3);
        textView = (TextView) findViewById(R.id.textView1);
        
        basketballCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checkedArray[0] = isChecked;
                textViewResetValue();
            }
        });
        footballCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checkedArray[1] = isChecked;
                textViewResetValue();
            }
        });
        pingpongCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checkedArray[2] = isChecked;
                textViewResetValue();
            }
        });
        
    }

    private void textViewResetValue() {
        String values = "";
        int sumChecked = 0;
        for (boolean val : checkedArray)
            if (val == true)
                sumChecked += 1;
        if (sumChecked == 0) 
            textView.setText("0 项选择");
        else {
            if (checkedArray[0] == true) values += ",篮球";
            if (checkedArray[1] == true) values += ",足球";
            if (checkedArray[2] == true) values += ",乒乓球";
            values = sumChecked + "项选择:" + values.substring(1);
            textView.setText(values);
        }
    }
}
MainActivity.java

效果:

注:Android有一个自己的log记录函数:Log.i()。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 RecyclerView 中实现多选删除,您需要在列表项布局中添加一个 CheckBox,并在 RecyclerView.Adapter 中实现选中和取消选中操作。以下是一个简单的实现示例: 1. 在列表项布局中添加一个 CheckBox: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <CheckBox android:id="@+id/checkbox" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <!-- 其他列表项内容 --> </LinearLayout> ``` 2. 在 RecyclerView.Adapter 中实现选中和取消选中操作: ```java public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { private List<Item> mItems; private SparseBooleanArray mSelectedItems; public MyAdapter(List<Item> items) { mItems = items; mSelectedItems = new SparseBooleanArray(); } public void toggleSelection(int position) { if (mSelectedItems.get(position, false)) { mSelectedItems.delete(position); } else { mSelectedItems.put(position, true); } notifyItemChanged(position); } public void clearSelections() { mSelectedItems.clear(); notifyDataSetChanged(); } public List<Item> getSelectedItems() { List<Item> items = new ArrayList<>(mSelectedItems.size()); for (int i = 0; i < mSelectedItems.size(); i++) { int position = mSelectedItems.keyAt(i); items.add(mItems.get(position)); } return items; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // 创建 ViewHolder } @Override public void onBindViewHolder(ViewHolder holder, int position) { Item item = mItems.get(position); // 绑定数据到 ViewHolder holder.textView.setText(item.getText()); holder.checkbox.setChecked(mSelectedItems.get(position, false)); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { toggleSelection(position); } }); } @Override public int getItemCount() { return mItems.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView textView; public CheckBox checkbox; public ViewHolder(View itemView) { super(itemView); textView = itemView.findViewById(R.id.textview); checkbox = itemView.findViewById(R.id.checkbox); } } } ``` 在上面的示例中,我们使用了 `SparseBooleanArray` 来记录选中的项。`toggleSelection()` 方法用于选中或取消选中指定位置的项,并通知 RecyclerView 更新列表项视图。`clearSelections()` 方法用于清除所有选中的项。`getSelectedItems()` 方法用于获取选中的项列表。 3. 在 Activity 或 Fragment 中处理删除操作: ```java public class MyActivity extends AppCompatActivity { private RecyclerView mRecyclerView; private MyAdapter mAdapter; // 初始化 RecyclerView 和 Adapter private void initRecyclerView() { mRecyclerView = findViewById(R.id.recyclerview); mAdapter = new MyAdapter(getItems()); mRecyclerView.setAdapter(mAdapter); } // 处理删除操作 private void handleDelete() { List<Item> selectedItems = mAdapter.getSelectedItems(); for (Item item : selectedItems) { deleteItem(item); } mAdapter.clearSelections(); mAdapter.notifyDataSetChanged(); } // 其他方法 } ``` 在上面的示例中,我们先调用 `getSelectedItems()` 方法获取选中的项列表,然后遍历该列表,调用 `deleteItem()` 方法删除每个选中的项。最后,我们清除所有选中的项,并调用 `notifyDataSetChanged()` 方法通知 RecyclerView 更新列表视图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值