1.不同颜色区分不同的RadioGroup
2.分别为RadioGroup和RadioButton添加监听器
3.实现选中第一个RadioGroup中的RadioButton时,第二个RadioGroup中的RadioButton也为选中状态。
结果:
1.结果显示,当点击RadioGroup中的RadioButton时。首先相应的是绑定在RadioButton上的监听器,然后
RadioGroup上的监听器再响应。
2.选中classButton后同时设置musicButton也为选中状态这时musicButton的监听器会
在classButton的监听器响应之后响应。与此不同的是,CheckBox在设置同样的操作时,被
设置为选中状态的CheckBox不会触发监听器。这一点从A02_CheckBox的设置中可以体现,具体
原因还不明白,至少从表面上看来是这样的。
Android 2.3.7真机效果:
activity_main.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:background="@drawable/lake"
android:orientation="vertical"
tools:context=".MainActivity" >
<RadioGroup
android:id="@+id/radioGroupOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/classButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上课"
android:textColor="#ff00ff" />
<RadioButton
android:id="@+id/sleepButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="睡觉"
android:textColor="#ff00ff" />
</RadioGroup>
<RadioGroup
android:id="@+id/radioGroupTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/musicButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="听歌"
android:textColor="#ffff00" />
<RadioButton
android:id="@+id/lolButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOL"
android:textColor="#ffff00" />
</RadioGroup>
</LinearLayout>
MainActivity.java
package com.haut.a03_radiogroup;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
/**
* 结果显示,当点击RadioGroup中的RadioButton时。首先相应的是绑定在RadioButton上的监听器,然后
* RadioGroup上的监听器再响应。
*
* 特点:选中classButton后同时设置musicButton也为选中状态这时musicButton的监听器会
* 在classButton的监听器响应之后响应。与此不同的是,CheckBox在设置同样的操作时,被
* 设置为选中状态的CheckBox不会触发监听器。这一点从A02_CheckBox的设置中可以体现,具体 原因还不明白,至少从表面上看来是这样的。
*/
public class MainActivity extends Activity {
// 声明RadioGroup和RadioButton
private RadioGroup radioGroupOne;
private RadioGroup radioGroupTwo;
private RadioButton classButton;
private RadioButton sleepButton;
private RadioButton musicButton;
private RadioButton lolButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 获取RadioGroup和RadioButton
// 第一组
radioGroupOne = (RadioGroup) findViewById(R.id.radioGroupOne);
classButton = (RadioButton) findViewById(R.id.classButtonId);
sleepButton = (RadioButton) findViewById(R.id.sleepButtonId);
// 第二组
radioGroupTwo = (RadioGroup) findViewById(R.id.radioGroupTwo);
musicButton = (RadioButton) findViewById(R.id.musicButtonId);
lolButton = (RadioButton) findViewById(R.id.lolButtonId);
RadioGroupListener groupListener = new RadioGroupListener();
RadioButtonListener buttonListener = new RadioButtonListener();
// 为第一组设置监听器
radioGroupOne.setOnCheckedChangeListener(groupListener);
classButton.setOnCheckedChangeListener(buttonListener);
sleepButton.setOnCheckedChangeListener(buttonListener);
// 为第二组设置监听器
radioGroupTwo.setOnCheckedChangeListener(groupListener);
musicButton.setOnCheckedChangeListener(buttonListener);
lolButton.setOnCheckedChangeListener(buttonListener);
}
// 创建RadioGroup监听器,用于监听RadioGroup中哪一个RadioButton被选中
class RadioGroupListener implements OnCheckedChangeListener {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (group.getId() == R.id.radioGroupOne) {
if (checkedId == classButton.getId()) {
Toast.makeText(MainActivity.this, "上课", Toast.LENGTH_SHORT)
.show();
musicButton.setChecked(true);
} else if (checkedId == sleepButton.getId()) {
Toast.makeText(MainActivity.this, "睡觉", Toast.LENGTH_SHORT)
.show();
lolButton.setChecked(true);
}
} else if (group.getId() == R.id.radioGroupTwo) {
if (checkedId == musicButton.getId()) {
Toast.makeText(MainActivity.this, "听歌", Toast.LENGTH_SHORT)
.show();
} else if (checkedId == lolButton.getId()) {
Toast.makeText(MainActivity.this, "LOL", Toast.LENGTH_SHORT)
.show();
}
}
}
}
// 创建RadioButton监听器,用于监听RadioButton的选中状态,
class RadioButtonListener implements
android.widget.CompoundButton.OnCheckedChangeListener {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (buttonView.getId() == R.id.classButtonId) {
if (isChecked) {
Toast.makeText(MainActivity.this, "上课已选!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "上课未选!",
Toast.LENGTH_SHORT).show();
}
} else if (buttonView.getId() == R.id.sleepButtonId) {
if (isChecked) {
Toast.makeText(MainActivity.this, "睡觉已选!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "睡觉未选!",
Toast.LENGTH_SHORT).show();
}
} else if (buttonView.getId() == R.id.musicButtonId) {
if (isChecked) {
Toast.makeText(MainActivity.this, "听歌已选!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "听歌未选!",
Toast.LENGTH_SHORT).show();
}
} else if (buttonView.getId() == R.id.lolButtonId) {
if (isChecked) {
Toast.makeText(MainActivity.this, "LOL已选!",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "LOL未选!",
Toast.LENGTH_SHORT).show();
}
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}