android:CheckBox方法

*CheckBox的方法:*
isChecked方法:是否选中
setChecked方法:设置方块状态
toggle方法:切换状态
setOnCheckedChangeListener方法:设置监听器

一个简单的checkbox例子:
MainActivity .java

public class MainActivity extends Activity {
    private CheckBox checkbox1;
    private CheckBox checkbox2;
    private CheckBox checkbox3;
    private CheckBox checkbox4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        checkbox1=(CheckBox)findViewById(R.id.checkbox1);
        checkbox2=(CheckBox)findViewById(R.id.checkbox2);
        checkbox3=(CheckBox)findViewById(R.id.checkbox3);
        checkbox4=(CheckBox)findViewById(R.id.checkbox4);

        checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if(isChecked){
                    setTitle(checkbox1.getText()+"被选中");
                }else {
                    setTitle(checkbox1.getText()+"取消选中");
                }
            }
        });
        checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if(isChecked){
                    setTitle(checkbox2.getText()+"被选中");
                }else {
                    setTitle(checkbox2.getText()+"取消选中");
                }
            }
        });
        checkbox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if(isChecked){
                    setTitle(checkbox3.getText()+"被选中");
                }else {
                    setTitle(checkbox3.getText()+"取消选中");
                }
            }
        });
        checkbox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                if(isChecked){
                    setTitle(checkbox4.getText()+"被选中");
                }else {
                    setTitle(checkbox4.getText()+"取消选中");
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    public void GetResult(View v){

        String result="";
        if(checkbox1.isChecked()){
            result+=checkbox1.getText()+"、";
        }
        if(checkbox2.isChecked()){
            result+=checkbox2.getText()+"、";
        }
        if(checkbox3.isChecked()){
            result+=checkbox3.getText()+"、";
        }
        if(checkbox4.isChecked()){
            result+=checkbox4.getText()+"、";
        }
        if(!"".equals(result)){
            result=result.substring(0,result.length()-1);
        }else{
            result="No one";
        }
        Toast.makeText(getApplicationContext(), result, 0).show();
    }
}

资源文件 main.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="vertical" >

<TextView
        android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/textview"
    />

    <CheckBox
        android:id="@+id/checkbox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/box1"
    />

    <CheckBox
        android:id="@+id/checkbox2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/box2"
    />

    <CheckBox
        android:id="@+id/checkbox3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/box3"
    />

    <CheckBox
        android:id="@+id/checkbox4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/box4"/>
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button" 
        android:onClick="GetResult"
        />
</LinearLayout>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值