CheckBox多选框

作者: dayang | 来源:发表于2016-12-01 10:42 被阅读234次

    CheckBox为CompoundButton子类

    实现CheckBox,并输出选中CheckBox结果
    CheckBox效果图.png
    <RadioGroup
         android:id="@+id/rgSport"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical">
         <CheckBox
             android:id="@+id/cbBasket"
             android:text="篮球"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"/>
         <CheckBox
             android:id="@+id/cbFoot"
             android:text="足球"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"/>
           <CheckBox
             android:id="@+id/cbBadminton"
             android:text="羽毛球"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content" />
          <CheckBox
             android:id="@+id/cbPing_pang"
             android:text="乒乓球"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"/>
    </RadioGroup>
    

    取得已经被选中CheckBox的值,输出。
    有两种方式:

    • 使用CheckBox的isChecked()方法
    • 实现CompoundButton.OnCheckedChangeListener接口

    使用ArrayList将所有CheckBox对象存储,取得值时,遍历ArrayList里面的CheckBox的isChecked()选中状态,进行布尔判断;
    部分代码片段

    //在Activity中定义成员变量List<CheckBox>
    List<CheckBox>list;
    private void initView(){
        //一般控件类也会在Activity中为成员变量
        CheckBox cbBasket= (CheckBox) findViewById(R.id.cbBasket);
        CheckBox cbFoot= (CheckBox) findViewById(R.id.cbFoot);
        CheckBox cbBadminton=(CheckBox)findViewById(R.id.cbBadminton);
        CheckBox cbPing_pang= (CheckBox)findViewById(R.id.cbPing_pang);
        list=new ArrayList<CheckBox>();
        list.add(cbBasket);
        list.add(cbFoot);
        list.add(cbBadminton);
        list.add(cbPing_pang);
    }
    //在Button的点击事件中,将所有选择的值输出
    public void onClick(View view){
        String result="";//用于存储CheckBox的值
        for(int i=0;i<list.size();i++){
        CheckBox box=list.get(i);
        if(box.isChecked()){
            result+=box.getText().toString();
    }
    }
        Toast.makeText(this,result,Toast.LENGTH_SHORT).show();
    }
    

    Activity实现CompoundButton.OnCheckedChangeListener接口,用HashMap存储CheckBox对象值,以CheckBox的id值作为key,接口方法中进行HashMap值的设置:

    //在Activity中定义成员变量
    HashMap<Integer,String>map;
    private void initView(){
        //一般控件类也会在Activity中为成员变量
        CheckBox cbBasket= (CheckBox) findViewById(R.id.cbBasket);
        CheckBox cbFoot= (CheckBox) findViewById(R.id.cbFoot);
        CheckBox cbBadminton=(CheckBox)findViewById(R.id.cbBadminton);
        CheckBox cbPing_pang= (CheckBox)findViewById(R.id.cbPing_pang);
        //Activity实现接口,所以Activity为接口对象
        cbBasket.setOnCheckedChangeListener(this);
        cbFoot.setOnCheckedChangeListener(this);
        cbBadminton.setOnCheckedChangeListener(this);
        cbPing_pang.setOnCheckedChangeListener(this);
        map=new HashMap<Integer,String>();
    }
    //由这个方法可以得出CheckBox是CompoundButton的子类
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b){    
        int id=compoundButton.getId();
        if(b){
              map.put(id,compoundButton.getText().toString());
        }else{
             if(map.containsKey(id)){
                 map.remove(id);
    }}}}
    //在Button的点击事件中,将所有选择的值输出
    public void onClick(View view){
        String result="";
        Iterator iterator=map.entryset().iterator();
        while(iterator.hasNext()){
               Map.EntrySet<Integer>entry= (Map.Entry<Integer, String>)iterator.next();
              result+=entry.getValue();
    }
        Toast.makeText(this,result,Toast.LENGTH_SHORT).show();
    }
    

    相关文章

      网友评论

        本文标题:CheckBox多选框

        本文链接:https://www.haomeiwen.com/subject/mortmttx.html