android怎么实现单选按钮功能

发布时间:2021-04-17 11:14:50 作者:小新
来源:亿速云 阅读:192

这篇文章主要介绍android怎么实现单选按钮功能,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

首先我们要明白实现这样一个效果需要哪几部?

android怎么实现单选按钮功能

1、在layout布局文件中建立一个文件,我起的名字为activity_radio.xml

代码为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <!--
   RadioButton 要想实现多选一的效果必须放到RadioGroup 中,否则无法实现多选一的效果.
   技巧:要面向RadioGroup 编程,不要面向RaidoButton 编程,否则将增加很大代码量
   android:orientation="vertical":执行按钮组的方向,默认值是vertical.
   RadioGroup的父类时LinearLayout,但方向的默认值不再是线性布局的水平方向了,而是改成了垂直方向.
   -->
  <RadioGroup
    android:id="@+id/radioGroup_gender"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    >
    <RadioButton
      android:id="@+id/radioButton_male"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal"
      android:checked="true"
      android:text="男" />
    <RadioButton
      android:id="@+id/radioButton_female"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center_horizontal"
      android:checked="false"
      android:text="女" />
  </RadioGroup>
</LinearLayout>

2、在MainActivity中实现细节的功能

package com.hsj.example.commoncontroldemo02;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity_bak06 extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

  private RadioGroup radioGroup_gender;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_radio);
    this.radioGroup_gender= (RadioGroup) this.findViewById(R.id.radioGroup_gender);
    this.radioGroup_gender.setOnCheckedChangeListener(this);

  }

  /**
   * 当单选按钮的状态发生变化时自动调用的方法
   * @param group 单选按钮所在的按钮组的对象
   * @param checkedId 用户选中的单选按钮的id值
   */
  @Override
  public void onCheckedChanged(RadioGroup group, int checkedId) {
    //得到用户选中的 RadioButton 对象
    RadioButton radioButton_checked= (RadioButton) group.findViewById(checkedId);
    String gender=radioButton_checked.getText().toString();
    Toast.makeText(this,gender,Toast.LENGTH_LONG).show();
    switch (checkedId){
      case R.id.radioButton_male:
        //当用户点击男性按钮时执行的代码
        System.out.println("===男性===");
        break;
      case R.id.radioButton_female:
        //当用户点击女性按钮时执行的代码
        System.out.println("===女性===");
        break;
    }
    System.out.println("===onCheckedChanged(RadioGroup group="+group+", int checkedId="+checkedId+")==");
  }
}

以上是“android怎么实现单选按钮功能”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. HTML实现RadioButton单选按钮的方法
  2. html单选按钮默认选中如何实现

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android

上一篇:如何使用Android实现BannerLayout图文轮播功能

下一篇:Android如何实现自动填充短信验证码

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》