广告
返回顶部
首页 > 资讯 > 移动开发 >Android程序开发中单选按钮(RadioGroup)的使用详解
  • 514
分享到

Android程序开发中单选按钮(RadioGroup)的使用详解

radiogroup按钮Android 2022-06-06 08:06:12 514人浏览 泡泡鱼
摘要

在还没给大家介绍单选按钮(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文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作