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