布局包括主界面和Listview的布局:
- <span style="font-size:16px;"><?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="horizontal" >
- <TextView android:id="@+id/item_tv"
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:gravity="center_vertical"/>
- <CheckBox android:id="@+id/item_cb"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:clickable="false"
- android:focusable="false"
- android:focusableInTouchMode="false"
- android:gravity="center_vertical"/>
- </LinearLayout></span>
- <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <LinearLayout android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView android:id="@+id/tv"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- <ListView android:id="@+id/lv"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- <LinearLayout android:orientation="horizontal"
- android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:layout_alignParentBottom="true"
- android:background="#000000">
- <Button android:id="@+id/bt_selectall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/selectall"/>
- <Button android:id="@+id/bt_cancelselectall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/cancelall"/>
- <Button android:id="@+id/bt_deselectall"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/deselectall"/>
- </LinearLayout>
- </RelativeLayout></span>
然后在代码里控制:
- <span style="font-size:16px;">package com.kingfly;
- import java.util.ArrayList;
- import com.kingfly.MyAdapter.ViewHolder;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.Button;
- import android.widget.ListView;
- import android.widget.TextView;
- public class ListViewWithCheckboxActivity extends Activity {
- /** Called when the activity is first created. */
- private ListView lv;
- private MyAdapter mAdapter;
- private ArrayList<String> list;
- private Button bt_selectall;
- private Button bt_cancel;
- private Button bt_deselectall;
- private int checkNum;
- private TextView tv_show;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- lv = (ListView) findViewById(R.id.lv);
- bt_selectall = (Button) findViewById(R.id.bt_selectall);
- bt_cancel = (Button) findViewById(R.id.bt_cancelselectall);
- bt_deselectall = (Button) findViewById(R.id.bt_deselectall);
- tv_show = (TextView) findViewById(R.id.tv);
- tv_show.setText("您已选中0项");
- list = new ArrayList<String>();
- initDate();
- mAdapter = new MyAdapter(list, this);
- lv.setAdapter(mAdapter);
- bt_selectall.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- // TODO Auto-generated method stub
- for (int i = 0; i < list.size(); i++) {
- MyAdapter.getIsSelected().put(i, true);
- }
- checkNum = list.size();
- dataChanged();
- }
- });
- bt_cancel.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- // TODO Auto-generated method stub
- for (int i = 0; i < list.size(); i++) {
- if (MyAdapter.getIsSelected().get(i)) {
- MyAdapter.getIsSelected().put(i, false);
- checkNum--;
- }
- }
- dataChanged();
- }
- });
- bt_deselectall.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- // TODO Auto-generated method stub
- for (int i = 0; i < list.size(); i++) {
- if (MyAdapter.getIsSelected().get(i)) {
- MyAdapter.getIsSelected().put(i, false);
- checkNum--;
- } else {
- MyAdapter.getIsSelected().put(i, true);
- checkNum++;
- }
- }
- dataChanged();
- }
- });
- lv.setOnItemClickListener(new OnItemClickListener() {
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- ViewHolder holder = (ViewHolder) arg1.getTag();
- holder.cb.toggle();
- MyAdapter.getIsSelected().put(arg2, holder.cb.isChecked());
- if (holder.cb.isChecked() == true) {
- checkNum++;
- } else {
- checkNum--;
- }
- tv_show.setText("您已选中"+checkNum+"项");
- }
- });
- }
- private void initDate() {
- for (int i = 0; i < 15; i++) {
- list.add("data" + " " + i);
- }
- }
- private void dataChanged() {
- mAdapter.notifyDataSetChanged();
- tv_show.setText("choose:" + checkNum);
- }
- }</span>
- <span style="font-size:16px;">package com.kingfly;
- import java.security.PublicKey;
- import java.util.ArrayList;
- import java.util.HashMap;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.CheckBox;
- import android.widget.TextView;
- public class MyAdapter extends BaseAdapter {
- private ArrayList<String> list;
- private static HashMap<Integer, Boolean> isSelected;
- private Context context;
- private LayoutInflater inflater = null;
- public MyAdapter(ArrayList<String> list, Context context) {
- this.context = context;
- this.list = list;
- inflater = LayoutInflater.from(context);
- isSelected = new HashMap<Integer, Boolean>();
- initDate();
- }
- private void initDate() {
- for (int i = 0; i < list.size(); i++) {
- getIsSelected().put(i, false);
- }
- }
- public int getCount() {
- // TODO Auto-generated method stub
- return list.size();
- }
- public Object getItem(int position) {
- // TODO Auto-generated method stub
- return list.get(position);
- }
- public long getItemId(int position) {
- // TODO Auto-generated method stub
- return position;
- }
- public View getView(int position, View convertView, ViewGroup parent) {
- // TODO Auto-generated method stub
- ViewHolder holder = null;
- if (convertView == null) {
- holder = new ViewHolder();
- convertView = inflater.inflate(R.layout.listviewitem, null);
- holder.tv = (TextView) convertView.findViewById(R.id.item_tv);
- holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
- convertView.setTag(holder);
- } else {
- holder = (ViewHolder) convertView.getTag();
- }
- holder.tv.setText(list.get(position));
- holder.cb.setChecked(getIsSelected().get(position));
- return convertView;
- }
- public static HashMap<Integer, Boolean> getIsSelected() {
- return isSelected;
- }
- public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
- MyAdapter.isSelected = isSelected;
- }
- class ViewHolder{
- TextView tv;
- CheckBox cb;
- }
- }
- </span>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.kingfly"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk android:minSdkVersion="15" />
- <application
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name" >
- <activity
- android:name=".ListViewWithCheckboxActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>