标签:
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" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_marginTop="10dp" android:text="已选中n项" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginLeft="26dp" android:layout_marginTop="10dp" android:text="全选" /> <ListView android:id="@+id/lv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button1" ></ListView> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/lv" android:layout_centerHorizontal="true" android:text="反选" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button2" android:layout_alignBottom="@+id/button2" android:layout_alignParentRight="true" android:text="全不选" /> </RelativeLayout>
item.xml
如果ListView的Item中包含CheckBox,由于CheckBox的焦点优先 级高于ListView,所以当选中ListView时,ListView将不会有选中的状态,解决的方法就是在checkbox组件中加一属 性:android:focusable="false"
<?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" > <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="20sp" android:text="data" /> <CheckBox android:id="@+id/check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:gravity="center_vertical" /> </LinearLayout>
MainActivity.class
package com.bwie.test; import java.util.ArrayList; import java.util.List; import com.bwie.adapter.MyAdapter; import com.bwie.adapter.MyAdapter.ViewHolder; import com.bwievo.Goods; import android.os.Bundle; import android.app.Activity; import android.view.Menu; 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 MainActivity extends Activity { private Button bt_all; private Button bt_cancel; private ListView listView; private TextView textView; private List<Goods> list; private Button bt_cancelall; private MyAdapter adapter; private int num;//选中的项数 private int sum;//选中项数的总和 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 找控件 bt_all = (Button) findViewById(R.id.button1); bt_cancel = (Button) findViewById(R.id.button2); listView = (ListView) findViewById(R.id.lv); textView = (TextView) findViewById(R.id.textView1); bt_cancelall = (Button) findViewById(R.id.button3); // 创建集合存放数据 list = new ArrayList<Goods>(); // 设置数据 initData(); // 创建适配器 adapter = new MyAdapter(MainActivity.this, list); // 设置适配器 listView.setAdapter(adapter); // 对全选按钮进行监听 bt_all.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub for (int i = 0; i < list.size(); i++) { list.get(i).setBo(true); // 刷新适配器并改变textview的值 notifyData(); } } }); // 对反选按钮进行监听 bt_cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub for (int i = 0; i < list.size(); i++) { if (list.get(i).getBo()) { list.get(i).setBo(false); } else { list.get(i).setBo(true); } // 刷新适配器并改变textview的值 notifyData(); } } }); // 对全不选按钮进行监听 bt_cancelall.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub for (int i = 0; i < list.size(); i++) { list.get(i).setBo(false); // 刷新适配器并改变textview的值 notifyData(); } } }); // 绑定listview的监听器 listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub // 取得ViewHolder对象,这样就省去了通过层层的findViewById去实例化我们需要的cb实例 ViewHolder holder = (ViewHolder) view.getTag(); // 改变CheckBox的状态 holder.box.toggle(); // 将CheckBox的选中状况记录下来 list.get(position).setBo(holder.box.isChecked()); // 调整选定条目 if (holder.box.isChecked() == true) { num++; sum += Integer.parseInt(list.get(position).getName()); } else { num--; sum -= Integer.parseInt(list.get(position).getName()); } // 用TextView显示 textView.setText("当前已选中" + num + "项,价格是" + sum + "元"); } }); } private void initData() { // TODO Auto-generated method stub for (int i = 0; i < 30; i++) { list.add(new Goods(i + "", false)); } } private void notifyData() { num = 0; sum = 0; adapter.notifyDataSetChanged(); for (int i = 0; i < list.size(); i++) { if (list.get(i).getBo()) { num++; sum += Integer.parseInt(list.get(i).getName()); } } textView.setText("当前已选中" + num + "项,总价为" + sum); } }
list集合的vo类 Goods.class
package com.bwievo; public class Goods { private String name; private boolean bo; public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean getBo() { return bo; } public void setBo(boolean bo) { this.bo = bo; } public Goods(String name, boolean bo) { super(); this.name = name; this.bo = bo; } @Override public String toString() { return "Goods [name=" + name + ", bo=" + bo + "]"; } }
标签:
原文地址:http://www.cnblogs.com/ldou/p/5386521.html