Android如何用checkBox实现单选

第一次写博客,不足的地方多多提出,会努力改进,谢谢!(ps:图片上传了不知道怎么看不见。。。)

1.布局文件activity_main.xml

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

           <!-- listView -->
           <ListView
             android:id="@+id/lv_single_list"
             android:layout_width="fill_parent"
             android:layout_height="400dp"
             android:cacheColorHint="#00000000"
            android:clickable="true"
            android:descendantFocusability="blocksDescendants"
            android:divider="#f80"
            android:dividerHeight="1.0dip"
            android:fadingEdge="none"
            android:fastScrollEnabled="true"
            android:scrollingCache="false" />
</LinearLayout>

2. listviewitem.xml


<?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:gravity="center"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_zxing_section_sequence"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:textSize="16sp" />

    <TextView
        android:id="@+id/tv_zxing_sectionname"
        android:layout_width="210dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:textSize="14sp" />

    <CheckBox
        android:id="@+id/item_cb_section"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</LinearLayout>


3.MainActivity.java



    private ListView listView;//listView 控件
    
    private Map<Integer, Boolean> isSelected;//判断CheckBox是否选中
    
    private List beSelectedData = new ArrayList();    //存放选中checkbox的集合
    
    ListAdapter adapter;//适配器
    
    private List cs = null;//显示listview数据
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) this.findViewById(R.id.lv_single_list);
        cs  = new ArrayList();
        cs.add("aaaaaa");
        cs.add("bbbbbb");
        cs.add("cccccc");
        cs.add("dddddd");
        cs.add("eeeeee");
        cs.add("ffffff");
        initList();
    }
    
    void initList(){
        
        if (cs == null || cs.size() == 0)
            return;
        if (isSelected != null)
            isSelected = null;
        isSelected = new HashMap<Integer, Boolean>();
        for (int i = 0; i < cs.size(); i++) {
            isSelected.put(i, false);
        }
        // 清除已经选择的项
        if (beSelectedData.size() > 0) {
            beSelectedData.clear();
        }
        adapter = new ListAdapter(this, cs);
        listView.setAdapter(adapter);
     

    /**
     * The list allows up to one choicelistview 选中样式
     */

  listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        adapter.notifyDataSetChanged();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
               Log.i("map", cs.get(position).toString());
            }
        });
        
    }
   

//适配器
    class ListAdapter extends BaseAdapter {

        private Context context;

        private List cs;

        private LayoutInflater inflater;//布局填充器

        public ListAdapter(Context context, List data) {
            this.context = context;
            this.cs = data;
            initLayoutInflater();
        }

        void initLayoutInflater() {
            inflater = LayoutInflater.from(context);
        }

        public int getCount() {
            return cs.size();
        }

        public Object getItem(int position) {
            return cs.get(position);
        }

        public long getItemId(int position) {
            return 0;
        }
    public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.listviewitem,null);
                holder = new ViewHolder();
                holder.checkBox = (CheckBox) convertView
                        .findViewById(R.id.item_cb_section);
                holder.tv_sequence = (TextView) convertView
                        .findViewById(R.id.tv_zxing_section_sequence);
                holder.tv_sectionname = (TextView) convertView
                        .findViewById(R.id.tv_zxing_sectionname);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }            
            holder.checkBox.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    // 当前点击的CheckBox
                    boolean cu = !isSelected.get(position);
                    // 先将所有的CheckBox置为FALSE
                    for(Integer p : isSelected.keySet()) {
                        isSelected.put(p, false);
                    }
                    // 再将当前选择CB的实际状态
                    isSelected.put(position, cu);
                    //通知改变
                    ListAdapter.this.notifyDataSetChanged();
                    beSelectedData.clear();
                    if(cu)
                        beSelectedData.add(cs.get(position));
                }
            });
            //控件内容
            holder.tv_sequence.setText(String.valueOf(position + 1));
            holder.tv_sectionname.setText(cs.get(position).toString());
            holder.checkBox.setChecked(isSelected.get(position));
            return convertView;
        }
    }
    /**
     * 就是一个持有者的类,他里面一般没有方法,只有属性,作用就是一个临时的储存器,
     * 把你getView方法中每次返回的View存起来,可以下次再用。
     * 这样做的好处就是不必每次都到布局文件中去拿到你的View,提高了效率
     * @author Administrator
     *
     */
    class ViewHolder {

        CheckBox checkBox;

        TextView tv_sequence;

        TextView tv_sectionname;

    }


源码地址:http://download.csdn.net/detail/u010904027/9140483


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值