Android CheckBox监听事件


1.在xml文件中定义CheckBox,一定要定义id

<CheckBox 
    android:id="@+id/beijing" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="北京" />

2.在对应的Java文件中声明对象并赋值

   //对控件对象进行声明 
        CheckBox beijing=null; 
 beijing=(CheckBox)findViewById(R.id.beijing);

3.设置监听事件

复制代码
  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()+"取消选中"); 
                    } 
                } 
            });
复制代码

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM