标签:
RadioGroup组件内部含有若干个RadioButton组件,每一个RadioButton对应一个选项,利用RadioGroup类似于做单选题。
RadioGroup可以理解为存放RadioButton的容器,他将多个RadioButton组织起来,形成一个组,而用户在选择时只能是组内的某一个RadioButton,所以用户直接操作的对象是RadioButton组件。
以下实例是首先选择一个选项,按提交按钮,会弹出一个消息框提示选择信息。
首先是布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/LinearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="main.test_radiogroup.MainActivity" > <TextView android:id="@+id/TextView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tv1" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="@string/rb1" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rb2" /> <RadioButton android:id="@+id/radio2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rb3" /> <RadioButton android:id="@+id/radio3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rb4"/> <RadioButton android:id="@+id/radio4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/rb5"/> </RadioGroup> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn" /> </LinearLayout>
其次是strings.xml文件:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Test_RadioGroup</string> <string name="action_settings">Settings</string> <string name="tv1">请选出您最喜欢的一道菜:</string> <string name="rb1">铁锅蛋</string> <string name="rb2">蜜汁两样</string> <string name="rb3">瓷坛羊肉</string> <string name="rb4">肉丝带底</string> <string name="rb5">活鱼活吃</string> <string name="btn">提交</string> </resources>
再次是android源文件:
package main.test_radiogroup;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.Toast;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity implements OnClickListener{
private RadioButton rbtn1=null,rbtn2=null,rbtn3=null,rbtn4=null,rbtn5=null;
private Button btn1=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rbtn1=(RadioButton)findViewById(R.id.radio0);
rbtn2=(RadioButton)findViewById(R.id.radio1);
rbtn3=(RadioButton)findViewById(R.id.radio2);
rbtn4=(RadioButton)findViewById(R.id.radio3);
rbtn5=(RadioButton)findViewById(R.id.radio4);
btn1=(Button)findViewById(R.id.button1);
btn1.setOnClickListener(MainActivity.this);
}
@Override
public void onClick(View arg0) {
String str="";
if(rbtn1.isChecked()) str=rbtn1.getText().toString();
else if(rbtn2.isChecked()) str=rbtn2.getText().toString();
else if(rbtn3.isChecked()) str=rbtn3.getText().toString();
else if(rbtn4.isChecked()) str=rbtn4.getText().toString();
else if(rbtn5.isChecked()) str=rbtn5.getText().toString();
else { }
Toast.makeText(MainActivity.this,"您选择的是"+str,Toast.LENGTH_LONG).show();//弹出选择消息框
}
}
最后是测试结果:

标签:
原文地址:http://my.oschina.net/u/2243176/blog/358369