public class MainActivity extends Activity {
private RadioGroup grop=null;
private RadioButton man=null;
private RadioButton woman=null;
private CheckBox game=null;
private CheckBox run=null;
private CheckBox surf=null;
private CheckBox football=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getComponent();
grop.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
RadioButton clickBotton= (RadioButton) findViewById(grop.getCheckedRadioButtonId());
String sex="性别为:"+clickBotton.getText().toString();
Toast t=Toast.makeText(MainActivity.this, sex, Toast.LENGTH_SHORT);
t.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 200, 60);
t.show();
}
});
}
private void getComponent() {
grop=(RadioGroup) this.findViewById(R.id.rGrop);
man=(RadioButton) this.findViewById(R.id.sexMan);
woman=(RadioButton) this.findViewById(R.id.sexWoMan);
game=(CheckBox) this.findViewById(R.id.game);
run=(CheckBox) this.findViewById(R.id.run);
surf=(CheckBox) this.findViewById(R.id.surf);
surf=(CheckBox) this.findViewById(R.id.football);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void findAllInfo(View v)
{
if(R.id.btn_confirm==v.getId())
{
StringBuffer sbuffer=new StringBuffer();
RadioButton clickBotton= (RadioButton) findViewById(grop.getCheckedRadioButtonId());
String sex="性别为:"+clickBotton.getText().toString();
sbuffer.append(sex);
sbuffer.append("爱好:");
for(int i=0;i<3;i++)
{
//爱好的ID是放在一起的
CheckBox cb=(CheckBox) findViewById(R.id.game+i);
if(cb.isChecked())
{
sbuffer.append(""+cb.getText()+",");
}
}
Toast.makeText(this, sbuffer.toString(), Toast.LENGTH_LONG).show();
}
}
}
..........................................................
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:id="@+id/nameText"
/>
<Button
android:id="@+id/btn_confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/nameText"
android:layout_below="@id/nameText"
android:text="@string/confirm"
android:onClick="findAllInfo"
/>
<Button
android:id="@+id/btn_confirm_shap"
android:layout_width="200dp"
android:layout_height="60dp"
android:layout_below="@id/btn_confirm"
android:layout_alignLeft="@id/btn_confirm"
android:background="@drawable/btn_confirm"
android:text="@string/confirm"
android:onClick="findAllInfo"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/btn_confirm_shap"
android:layout_alignLeft="@id/btn_confirm_shap"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sex"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/rGrop"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sexMan"
android:text="@string/man"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/sexWoMan"
android:text="@string/woman"
/>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/like"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/game"
android:text="@string/game"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/surf"
android:text="@string/surf"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/run"
android:text="@string/run"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/football"
android:text="@string/football"
/>
</LinearLayout>
</RelativeLayout>
btn_confirm.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/btn_confirm_shap_down"></item>
<item android:state_pressed="false" android:drawable="@drawable/btn_confirm_shap_up"></item>
</selector>
btn_confirm_shap_up.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="5dp"></corners>
<stroke
android:color="#000000"
android:width="2dp"
android:dashWidth="3dp"
/>
<gradient
android:type="linear"
android:startColor="#ffffff"
android:endColor="#ffff00"
android:angle="45"
></gradient>
</shape>
btn_confirm_shap_down.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners android:radius="5dp"></corners>
<stroke
android:color="#000000"
android:width="2dp"
android:dashWidth="3dp"
/>
<gradient
android:type="linear"
android:startColor="#ffffff"
android:endColor="#ffff00"
android:angle="45"
></gradient>
</shape>