android CheckBox控件的定义及事件监听

</pre><p>CheckBox控件的定义,main.xml内容如下:</p><p><pre name="code" class="html"> <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<EditText 
    android:id="@+id/editText1"   
    android:layout_width="fill_parent"  
    android:layout_height="wrap_content"  
    android:text="请选择" 
    /> 
<CheckBox 
    android:id="@+id/beijing" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="北京" 
    /> 
<CheckBox 
    android:id="@+id/shanghai" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="上海" 
    /> 
<CheckBox 
    android:id="@+id/shenzhen" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="深圳" 
    /> 
</LinearLayout> 

activity CheckBoxTest.java内容如下:

    package checkbox.pack; 
     
    import android.app.Activity; 
    import android.os.Bundle; 
    import android.widget.CheckBox; 
    import android.widget.CompoundButton; 
    import android.widget.EditText; 
     
    public class CheckBoxTest extends Activity { 
         
        //对控件对象进行声明 
        CheckBox beijing=null; 
        CheckBox shanghai=null; 
        CheckBox shenzhen=null; 
        EditText editText1=null; 
        @Override 
        public void onCreate(Bundle savedInstanceState) { 
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.main); 
            //通过控件的ID来得到代表控件的对象 
            beijing=(CheckBox)findViewById(R.id.beijing); 
            shanghai=(CheckBox)findViewById(R.id.shanghai); 
            shenzhen=(CheckBox)findViewById(R.id.shenzhen); 
            editText1=(EditText)findViewById(R.id.editText1); 
            //给CheckBox设置事件监听 
            beijing.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ 
                @Override 
                public void onCheckedChanged(CompoundButton buttonView, 
                        boolean isChecked) { 
                    // TODO Auto-generated method stub 
                    if(isChecked){ 
                        editText1.setText(buttonView.getText()+"选中"); 
                    }else{ 
                        editText1.setText(buttonView.getText()+"取消选中"); 
                    } 
                } 
            }); 
            shanghai.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ 
                @Override 
                public void onCheckedChanged(CompoundButton buttonView, 
                        boolean isChecked) { 
                    // TODO Auto-generated method stub 
                    if(isChecked){ 
                        editText1.setText(buttonView.getText()+"选中"); 
                    }else{ 
                        editText1.setText(buttonView.getText()+"取消选中"); 
                    } 
                } 
            }); 
            shenzhen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ 
                @Override 
                public void onCheckedChanged(CompoundButton buttonView, 
                        boolean isChecked) { 
                    // TODO Auto-generated method stub 
                    if(isChecked){ 
                        editText1.setText(buttonView.getText()+"选中"); 
                    }else{ 
                        editText1.setText(buttonView.getText()+"取消选中"); 
                    } 
                } 
            }); 
        } 
    } 

android怎么把checkbox状态设置为选中状态

android:checked="true"

通过checkbox.isChecked();是判断后是否选中,

checkbox.setChecked(true|false)是赋某个checkbox选中。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值