修改velue文件
=>>string.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, RadioButtonActivity!</string>
- <string name="app_name">RadioButton</string>
- <string name="radB1">RadioButton1</string>
- <string name="radB2">RadioButton2</string>
- <string name="radB3">RadioButton3</string>
- </resources>
修改Activity文件
=>>RadioButtonProject.java
- package com.radioButton;
- import Android.app.Activity;
- import android.os.Bundle;
- import android.widget.RadioButton;
- import android.widget.RadioGroup;
- import android.widget.RadioGroup.OnCheckedChangeListener;
- import android.widget.Toast;
- public class RadioButtonActivity extends Activity implements OnCheckedChangeListener{
- private RadioButton rb1,rb2,rb3;
- private RadioGroup rg;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- rb1 = (RadioButton) findViewById(R.id.rb1);
- rb2 = (RadioButton) findViewById(R.id.rb2);
- rb3 = (RadioButton) findViewById(R.id.rb3);
- rg = (RadioGroup) findViewById(R.id.radGrp);
- rg.setOnCheckedChangeListener(this);//将单选组绑定监听器
- }
- //重写监听器函数
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- if(group==rg){
- String rbName = null;
- if(checkedId == rb1.getId()){
- rbName = rb1.getText().toString();
- } else if (checkedId == rb2.getId()){
- rbName = rb2.getText().toString();
- } else if(checkedId == rb3.getId()){
- rbName = rb3.getText().toString();
- }
- Toast.makeText(this,"选择了下标为“"+ rbName + "”的单选按钮",
- Toast.LENGTH_LONG).show();
- }
- }
- }
更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11
