android 常见组件,android常用组件之RadioGroup

本文介绍了一个Android应用程序的实例,该实例使用RadioGroup组件来实现单选功能。用户从多个RadioButton选项中选择一项,点击提交按钮后,程序会弹出一个消息框显示所选内容。主要涉及的组件包括RadioButton、RadioGroup和Button,通过监听按钮点击事件获取选中项,并展示在Toast中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RadioGroup组件可以容纳若干个RadioButton组件,每一个RadioButton对应一个选项,利用RadioGroup类似于做单选题。

RadioGroup可以理解为存放RadioButton的容器,他将多个RadioButton组织起来,形成一个组,而用户在选择时只能是组内的某一个RadioButton,所以用户直接操作的对象是RadioButton组件。

以下实例是首先选择一个选项,按提交按钮,会弹出一个消息框提示选择信息。

首先是布局文件:

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" >

android:id="@+id/TextView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/tv1" />

android:id="@+id/radioGroup1"

android:layout_width="wrap_content"

android:layout_height="wrap_content" >

android:id="@+id/radio0"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:checked="true"

android:text="@string/rb1" />

android:id="@+id/radio1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/rb2" />

android:id="@+id/radio2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/rb3" />

android:id="@+id/radio3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/rb4"/>

android:id="@+id/radio4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/rb5"/>

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/btn" />

其次是strings.xml文件:

Test_RadioGroup

Settings

请选出您最喜欢的一道菜:

铁锅蛋

蜜汁两样

瓷坛羊肉

肉丝带底

活鱼活吃

提交

再次是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();//弹出选择消息框

}

}

最后是测试结果:

023971f1f6f1c4d265c888894d6c62bb.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值