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

在还没给大家介绍单选按钮(RadioGroup)的使用,先给大家展示下效果图吧:

a9e1d26569a5d44bff99a31b68d13913.png

xml文件

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

android:id="@+id/txt"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="您的性别为"/>

android:id="@+id/sex"

android:layout_width="fill_parent"

android:layout_height="wrap_content">

android:id="@+id/male"

android:text="男"/>

android:id="@+id/female"

android:text="女"/>

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)的使用详解,希望对大家有所帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值