<?xml version="1.0" encoding="utf-8"?>
<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.example.pengtuanyuan.demo0112.MainActivity">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/textView1" />
<RadioGroup
android:orientation="vertical"
android:id="@+id/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female"/>
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"/>
</RadioGroup>
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swimming"/>
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/running"/>
<CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/fishing"/>
</LinearLayout>
--------------------------------------------------------
package com.example.pengtuanyuan.demo0112;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private RadioGroup radioGroup=null;
private RadioButton radioButton1=null;
private RadioButton radioButton2=null;
private CheckBox swimBox=null;
private CheckBox runBox=null;
private CheckBox fishBox=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup=(RadioGroup)findViewById(R.id.genderGroup);
radioButton1=(RadioButton)findViewById(R.id.radioButton1);
radioButton2=(RadioButton)findViewById(R.id.radioButton2);
swimBox=(CheckBox)findViewById(R.id.checkbox1);
runBox=(CheckBox)findViewById(R.id.checkbox2);
fishBox=(CheckBox)findViewById(R.id.checkbox3);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
if (radioButton1.getId()==checkedId){
System.out.println("checked is female");
Toast.makeText(MainActivity.this,"女",Toast.LENGTH_SHORT).show();
}else if (radioButton2.getId()==checkedId) {
System.out.println("checked is male");
Toast.makeText(MainActivity.this,"男",Toast.LENGTH_SHORT).show();
}
}
});
swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked){
System.out.println("checked is swimming");
}else {
System.out.println("checked is unchecked");
}
}
});
runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked){
System.out.println("checked is running");
}else {
System.out.println("checked is unchecked");
}
}
});
fishBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked){
System.out.println("checked is fishing");
}else {
System.out.println("checked is unchecked");
}
}
});
}
}
1.png
网友评论