<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ashzheng.testcodingke.Main2Activity">
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="第一个" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="第二个" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="第三个" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:text="第一个RadioButton" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:text="第二个RadioButton" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@null"
android:text="第三个RadioButton" />
</RadioGroup>
</LinearLayout>
<pre name="code" class="java">package com.ashzheng.testcodingke;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.Toast;
public class Main2Activity extends Activity implements CompoundButton.OnCheckedChangeListener{
private CheckBox cb1, cb2, cb3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
cb1 = (CheckBox) findViewById(R.id.checkBox1);
cb2 = (CheckBox) findViewById(R.id.checkBox2);
cb3 = (CheckBox) findViewById(R.id.checkBox3);
cb1.setOnCheckedChangeListener(this);
cb2.setOnCheckedChangeListener(this);
cb3.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.checkBox1:
if(isChecked)
Toast.makeText(this, "你选定了CheckBox1", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "你取消了CheckBox1", Toast.LENGTH_SHORT).show();
break;
case R.id.checkBox2:
if(isChecked)
Toast.makeText(this, "你选定了CheckBox2", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "你取消了CheckBox2", Toast.LENGTH_SHORT).show();
break;
case R.id.checkBox3:
if(isChecked)
Toast.makeText(this, "你选定了CheckBox3", Toast.LENGTH_SHORT).show();
else
Toast.makeText(this, "你取消了CheckBox3", Toast.LENGTH_SHORT).show();
break;
}
}
}