CheckBox的全选或反选

时间:2021-11-05 10:30:22

实现的步骤如下:


一、创建项目

  1.1 项目构建图:

CheckBox的全选或反选

   1.2  代码展示如下:

        1.2.1 MainActivity .class文件

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

private CheckBox check_all;
private ListView lView;
private CheckAdapter checkAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

check_all = (CheckBox) findViewById(R.id.check_all);
lView = (ListView) findViewById(R.id.list_view);

List<String> list = new ArrayList<String>();
for (int i = 0; i < 15; i++) {
list.add("这是条目"+i);
}

checkAdapter = new CheckAdapter(getApplicationContext(),list);
lView.setAdapter(checkAdapter);

check_all.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
boolean flag = check_all.isChecked();

for (int i = 0; i < checkAdapter.getSelect().size(); i++) {
checkAdapter.getSelect().set(i, flag);
}
checkAdapter.notifyDataSetChanged();
}
});

}


private class CheckAdapter extends BaseAdapter {
private List<String> list;
private Context context;
private LinkedList<Boolean> linkedList = new LinkedList<Boolean>();

public CheckAdapter(Context context, List<String> list) {
// TODO Auto-generated constructor stub
this.list = list;
this.context = context;
//初始化
for (int i = 0; i < list.size(); i++) {
getSelect().add(false);
}
}

private List<Boolean> getSelect() {
return linkedList;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = View.inflate(context, R.layout.item, null);
}
CheckBox ck = (CheckBox) convertView.findViewById(R.id.ck);
TextView tView = (TextView) convertView.findViewById(R.id.tv);

tView.setText(list.get(position));

ck.setChecked(linkedList.get(position));
//不能用onCheckChangedListner()复用的时候
ck.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
linkedList.set(position, ! linkedList.get(position));
if (linkedList.contains(false)) {
check_all.setChecked(false);
}else {
check_all.setChecked(true);
}
//刷新
notifyDataSetChanged();
}
});
return convertView;
}

}

}

1.3  布局文件

    1.3.1   activity_main.xml

<RelativeLayout 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" >

<ListView
android:layout_above="@+id/check_all"
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>

<CheckBox
android:id="@+id/check_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="全选" />

</RelativeLayout>

      1.3.2    item.xml

<?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="horizontal" >

<CheckBox
android:id="@+id/ck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/tv"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

下载地址    


     CSDN下载地址:

         点击下载