Android单选按钮RadioButton的使用详解

 更新时间:2019年03月29日 09:20:36   作者:徐刘根  
今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

RadioButton是最普通的UI组件之一,继承了Button类,可以直接使用Button支持的各种属性和方法。

RadioButton与普通按钮不同的是,它多了一个可以选中的功能,可额外指定一个android:checked属性,该属性可以指定初始状态时是否被选中,其实也可以不用指定,默认初始状态都不选中。

使用RadioButton必须和单选框RadioGroup一起使用,在RadioGroup中放置RadioButton,通过setOnCheckedChangeListener( )来响应按钮的事件;

(1)选用radioGroup的图标

<RelativeLayout 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"
  android:paddingBottom="@dimen/activity_vertical_margin"
  android:paddingLeft="@dimen/activity_horizontal_margin"
  android:paddingRight="@dimen/activity_horizontal_margin"
  android:paddingTop="@dimen/activity_vertical_margin"
  tools:context=".MainActivity" >
  <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="44dp"
    android:text="性别:"
    android:textSize="20dp" />
  <RadioGroup
    android:id="@+id/radioGroup1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/textView1"
    android:layout_marginLeft="21dp"
    android:layout_toRightOf="@+id/textView1"
    android:orientation="horizontal" >
    <RadioButton
      android:id="@+id/radio0"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:checked="true"
      android:onClick="onRadioButtonClicked"
      android:text="男" />
    <RadioButton
      android:id="@+id/radio1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:onClick="onRadioButtonClicked"
      android:text="女" />
    <RadioButton
      android:id="@+id/radio2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:onClick="onRadioButtonClicked"
      android:text="保密" />
  </RadioGroup>
</RelativeLayout>

(2)控制的类是

package com.lc.radiobutton;
import com.example.radiobutton.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 }
 /*
 * 设置radio的点击事件,当点击的时候显示文字
 */
 public void onRadioButtonClicked(View view) {
 RadioButton button = (RadioButton) view;
 boolean isChecked = button.isChecked();
 switch (view.getId()) {
 case R.id.radio0:
  if (isChecked) {
  Toast.makeText(MainActivity.this, button.getText(), 1).show();
  }
  break;
 case R.id.radio1:
  if (isChecked) {
  Toast.makeText(MainActivity.this, button.getText(), 1).show();
  }
  break;
 case R.id.radio2:
  if (isChecked) {
  Toast.makeText(MainActivity.this, button.getText(), 1).show();
  }
  break;
 default:
  break;
 }
 }
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
 // Inflate the menu; this adds items to the action bar if it is present.
 getMenuInflater().inflate(R.menu.main, menu);
 return true;
 }
}

(3)显示结果,当点击的时候显示文字

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

相关文章

  • Android RxJava异步数据处理库使用详解

    Android RxJava异步数据处理库使用详解

    RxJava是一种异步数据处理库,也是一种扩展的观察者模式。对于Android开发者来说,使用RxJava时也会搭配RxAndroid,它是RxJava针对Android平台的一个扩展,用于Android 开发,它提供了响应式扩展组件,使用RxAndroid的调度器可以解决Android多线程问题
    2022-11-11
  • Android gradient 使用小结

    Android gradient 使用小结

    在Android中使用gradient(渐变)通常是通过drawable文件来设置背景,下面是可以直接用的几种用法汇总,包括线性渐变、径向渐变、扫描渐变(sweep)等,感兴趣的朋友一起看看吧
    2025-04-04
  • Android开发技巧之ViewStub控件惰性装载

    Android开发技巧之ViewStub控件惰性装载

    布局文件中的控件并不一定在程序启动时全都用到,有一些控件只在特定的情况下才会被使用到;我们急需一种机制来改变<include>标签的这种行为,只在需要时装载控件。这种机制就是本节要介绍的ViewStub控件
    2013-01-01
  • Android Compose状态改变动画animateXxxAsState使用详解

    Android Compose状态改变动画animateXxxAsState使用详解

    这篇文章主要为大家介绍了Android Compose状态改变动画animateXxxAsState使用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11
  • Android实现摇一摇简单功能

    Android实现摇一摇简单功能

    这篇文章主要为大家详细介绍了Android实现摇一摇简单功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-03-03
  • Android指纹解锁方法解析

    Android指纹解锁方法解析

    这篇文章主要为大家详细解析了Android指纹解锁方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • Android MVP模式面向接口写法

    Android MVP模式面向接口写法

    这篇文章主要介绍了Android MVP模式面向接口写法,MVP模式也出来好几年了,很成熟所以也导致写法有很多种,google提供了多种mvp模式,但我今天只讲解最简单的面向接口,需要详细了解可以参考下文
    2023-05-05
  • android开发实践之ndk编译命令简单示例

    android开发实践之ndk编译命令简单示例

    这篇文章主要给大家介绍了在android中ndk编译命令使用的相关资料,文中详细介绍了ndk-build命令行参数,并通过简单的示例代码给大家介绍了如何编写 .c 文件,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-06-06
  • Android自定义控件如何在XML文件中使用自定义属性

    Android自定义控件如何在XML文件中使用自定义属性

    这篇文章主要为大家介绍了Android自定义控件之如何在XML文件中使用自定义属性示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-04-04
  • Android SurfaceView与TextureView使用方法详细讲解

    Android SurfaceView与TextureView使用方法详细讲解

    SurfaceView和TextureView都继承View,与普通的View不同的是,它俩可以在独立线程中绘制渲染,性能更高,所以常被应用在对绘制速率要求比较高的场景,比如相机预览,视频播放等等
    2022-10-10

最新评论