在还没给大家介绍单选按钮(RadioGroup)的使用,先给大家展示下效果图吧: xml文件 <LinearLayout xmlns:Android="Http:
在还没给大家介绍单选按钮(RadioGroup)的使用,先给大家展示下效果图吧:
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"
tools:context=".MainActivity"
android:orientation="vertical">
<TextView
android:id="@+id/txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="您的性别为"/>
<RadioGroup
android:id="@+id/sex"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/male"
android:text="男"/>
<RadioButton
android:id="@+id/female"
android:text="女"/>
</RadioGroup>
</LinearLayout>
java文件
public class
MainActivity extends Activity {
private TextView txt=null;
private RadioGroup sex=null;
private RadioButton male=null;
private RadioButton female=null;
@Override
protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.txt=(TextView)
super.findViewById(R.id.txt);
this.sex=(RadioGroup)
super.findViewById(R.id.sex);
this.male=(RadioButton)
super.findViewById(R.id.male);
this.female=(RadioButton)
super.findViewById(R.id.female);
this.sex.setOnCheckedChangeListener(new
OnCheckedChangeListenerImp());
} private class
OnCheckedChangeListenerImp implements
OnCheckedChangeListener{
public void
onCheckedChanged(RadioGroup group, int checkedId)
{ String temp=null;
if(MainActivity.this.male.getId()==checkedId){
temp="男";
} else if(MainActivity.this.female.getId()==checkedId){
temp="女";
} MainActivity.this.txt.setText("您的性别是"+temp);
} }
以上所述是小编给大家介绍的Android程序开发中单选按钮(RadioGroup)的使用详解,希望对大家有所帮助!
您可能感兴趣的文章:Android控件系列之RadioButton与RadioGroup使用方法android RadioGroup的使用方法android自定义RadioGroup可以添加多种布局的实现方法Android开发之RadioGroup的简单使用与监听示例Android RadioGroup和RadioButton控件简单用法示例Android RadioGroup 设置某一个选中或者不可选中的方法Android使用RadioGroup实现底部导航栏Android实现界面内嵌多种卡片视图(ViewPager、RadioGroup)Android 自定义View实现任意布局的RadioGroup效果Android基础控件RadioGroup使用方法详解
--结束END--
本文标题: Android程序开发中单选按钮(RadioGroup)的使用详解
本文链接: https://www.lsjlt.com/news/25528.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0