Home

安卓Android按钮Button点击和复选框CheckBox选中的监控触发事件

CheckBox复选框和按钮Button的定义,main.xml内容如下:

<CheckBox
    android:id="@+id/checkbox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="复选框1"
/>
<Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="20dp"
        android:text="按钮1"
/>

JAVA代码如下:

btn1 =(Button)findViewById(R.id.button1);
btn1.setOnClickListener(new Button.OnClickListener(){public void onClick(View arg0) {这里输入点击Button按钮触发的事件}});
CheckBox被选中或取消选中触发事件:
checkbox1=(CheckBox)findViewById(R.id.checkbox1);
b5.setOnCheckedChangeListener(new OnCheckedChangeListener(){if(checkbox1.isChecked()){这里输入CheckBox复选框选中时触发的事件}else{这里输入CheckBox复选框取消选中时触发的事件}});

附:Button超简单监控点击事件

按钮Button的定义,main.xml内容如下:

<Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="20dp"
        android:text="按钮1"
        android:onClick="btnOnClick"
/>

JAVA代码如下:

public void btnOnClick(){
这里输入
点击Button按钮触发的事件
}