activity_main.xml的代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/swim"
android:checked="true"
/>
<CheckBox
android:id="@+id/xiaqi"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/xiaqi" />
<CheckBox
android:id="@+id/look"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="@string/look" />
<RadioGroup
android:id="@+id/RGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择你的性别"
android:singleLine="true"
/>
<RadioButton
android:id="@+id/man"
android:layout_width="50dp"
android:layout_height="match_parent"
android:text="男"
android:checked="true"
/>
<RadioButton
android:id="@+id/women"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:text="女"
/>
</RadioGroup>
</LinearLayout>
MainActivity.java中的代码
package com.example.editview;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends Activity
{
private RadioGroup Rgroup=null;
private RadioButton man=null;
private RadioButton wommen=null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Button bt;
// Button bt1;
// bt=(Button)findViewById(R.id.btn);
// bt1=(Button)findViewById(R.id.btn1);
// bt.setOnClickListener(listener1);
// bt1.setOnClickListener(listener1);
CheckBox bb;
CheckBox cc;
CheckBox dd;
bb=(CheckBox)findViewById(R.id.swim);
cc=(CheckBox)findViewById(R.id.xiaqi);
dd=(CheckBox)findViewById(R.id.look);
bb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, "你选择了游泳", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "你没有选择游泳", Toast.LENGTH_LONG).show();
}
}
});
cc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, "你选择了下棋", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "你没选择了下棋", Toast.LENGTH_LONG).show();
}
}
});
dd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
// TODO Auto-generated method stub
if(isChecked)
{
Toast.makeText(MainActivity.this, "你选择了看电视", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(MainActivity.this, "你没选择了看电视", Toast.LENGTH_LONG).show();
}
}
});
Rgroup =(RadioGroup)findViewById(R.id.RGroup);
man=(RadioButton)findViewById(R.id.man);
wommen=(RadioButton)findViewById(R.id.women);
Rgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
if(arg1==man.getId())
{
System.out.println("man");
}
else if (arg1==wommen.getId())
{
System.out.println("wommen");
}
}
});
}
}