您的位置:首页 > 移动开发 > Android开发

android基本控件RadioButton/CheckBox

2017-07-20 12:22 435 查看
MainActivity.xml

<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="${relativePackage}.${activityClass}" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical" >

<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female" >
</RadioButton>

<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male" >
</RadioButton>
</RadioGroup>

<CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swim" />

<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/run" />

<CheckBox
android:id="@+id/write"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/write" />

</LinearLayout>


MainActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends Activity {
private  RadioGroup radioGroup;
private  RadioButton female;
private RadioButton male;
private CheckBox run,swim,write;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
female=(RadioButton)findViewById(R.id.female);
male=(RadioButton)findViewById(R.id.male);
run=(CheckBox)findViewById(R.id.run);
swim=(CheckBox)findViewById(R.id.swim);
write=(CheckBox)findViewById(R.id.write);

/*监听器*/
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if(female.getId() == checkedId){
System.out.println("女生");
Toast.makeText(MainActivity.this, "女生", Toast.LENGTH_LONG).show();
}
else if(male.getId()==checkedId){
System.out.println("男生");
Toast.makeText(MainActivity.this, "男生", Toast.LENGTH_LONG).show();
}

}
});

/*给每个checkBox都要加监听器*/
run.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Toast.makeText(MainActivity.this, "run", Toast.LENGTH_LONG).show();
}

}

});

write.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Toast.makeText(MainActivity.this, "write", Toast.LENGTH_LONG).show();
}

}

});

swim.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
Toast.makeText(MainActivity.this, "swim", Toast.LENGTH_LONG).show();
}

}

});

}
}


string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Activity_04</string>
<string name="hello_world">单选和多选</string>
<string name="female">female</string>
<string name="male">male</string>
<string name="swim">swim</string>
<string name="run">run</string>
<string name="write">write</string>

</resources>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: