Android studio实现单选按钮

本文实例为大家分享了Android studio实现单选按钮的具体代码,供大家参考,具体内容如下

创建空activity
编辑activity_main.xml文件

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center"
    tools:context=".MainActivity">
    <TextView
        android:id="@+id/chooseTxt"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/text1"
        android:textColor="@color/colorblack"
        android:textSize="30sp" />
        //定义RaidGroup是要注意属性添加的位置
    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center"
        >
        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:button="@null"
            android:drawableRight="@android:drawable/btn_radio"
            android:text="@string/text2"
            />
        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="@string/text3"
            />
    </RadioGroup>

    <Button
        android:id="@+id/ClearBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/text4" />
    <Button
        android:id="@+id/AddBtn"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/text5"
         />
</androidx.appcompat.widget.LinearLayoutCompat>

还有strings.xml文件,代码如下:

<resources>
    <string name="app_name">My App</string>
    <string name="text1">我选择的是...?</string>
    <string name="text2">按钮1</string>
    <string name="text3">按钮2</string>
    <string name="text4">清除选中</string>
    <string name="text5">添加子项</string>
</resources>

再是MainActivity.java文件,代码如下:

package com.example.myapp;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private RadioGroup  radioGroup;
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private Button radioClearBtn;
    private Button radioAddBtn;
    private TextView chooseTxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioButton1 = findViewById(R.id.radioButton1);
        radioButton2 = findViewById(R.id.radioButton2);
        radioGroup= findViewById(R.id.radioGroup);
        //设置选中变换监听
        radioGroup.setOnCheckedChangeListener(onCheckedChangeListener);
        //分别为两个按钮设置点击监听

        radioClearBtn = findViewById(R.id.ClearBtn);
        radioClearBtn.setOnClickListener(onClickListener);

        radioAddBtn = findViewById(R.id.AddBtn);
        radioAddBtn.setOnClickListener(onClickListener);

        chooseTxt = findViewById(R.id.chooseTxt);

    }
    //onCheckedChangeListener()方法
    private  OnCheckedChangeListener onCheckedChangeListener=new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
        //定义id并赋值被选中的单选按钮的id
            int id = group.getCheckedRadioButtonId();
            switch (id) {
                case R.id.radioButton1:
                    chooseTxt.setText("我选择的是:" + radioButton1.getText());
                    break;
                case R.id.radioButton2:
                    chooseTxt.setText("我选择的是:" + radioButton2.getText());
                    break;
                default:
                    chooseTxt.setText("我选择的是:新增");
                    break;
             }
        }
    };
    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.ClearBtn:
                        radioGroup.check(-1);//清除选项
                        chooseTxt.setText("我选择的是...?");
                        break;
                    case R.id.AddBtn:
                        RadioButton newRadio = new RadioButton(MainActivity.this);
                        //将新增的radiobutton加入到radioGroup中
                        newRadio.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT));
                        newRadio.setText("新增");
                        radioGroup.addView(newRadio, radioGroup.getChildCount());
                        break;
                    default:
                        break;
                }
        }
    };
}

运行结果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • Android单选按钮RadioButton的使用详解

    RadioButton是最普通的UI组件之一,继承了Button类,可以直接使用Button支持的各种属性和方法. RadioButton与普通按钮不同的是,它多了一个可以选中的功能,可额外指定一个android:checked属性,该属性可以指定初始状态时是否被选中,其实也可以不用指定,默认初始状态都不选中. 使用RadioButton必须和单选框RadioGroup一起使用,在RadioGroup中放置RadioButton,通过setOnCheckedChangeListener( )来响

  • android实现单选按钮功能

    在我们平时在注册个人信息的时候,经常会让我们选择是男生还是女生,那么这个单选框在Android中是怎么实现的呢?现在我们就来学习一下吧 首先我们要明白实现这样一个效果需要哪几部? 1.在layout布局文件中建立一个文件,我起的名字为activity_radio.xml 代码为: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schem

  • Android单选按钮RadioButton的使用方法

    单选按钮要在一组中选择一项,并且不能多选. 同一组RadioButton要放在同一个RadioGroup节点下. RadioButton默认未选中,点击后选中但是再次点击不会取消选中. RadioButton经常会更换按钮图标,如果通过button属性变更图标,那么图标与文字就会挨得很近.为了拉开图标与文字之间的距离,得换成drawableLeft属性展示新图标(不要忘记把button改为@null),再设置drawablePadding即可指定间隔距离. 复现代码时出现了一个错误,处理单选按钮

  • Android单选按钮对话框用法实例分析

    本文实例讲述了Android单选按钮对话框用法.分享给大家供大家参考.具体如下: main.xml布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:l

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

    在还没给大家介绍单选按钮(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_heig

  • Android实现单选按钮

    本文实例为大家分享了Android实现单选按钮的具体代码,供大家参考,具体内容如下 单选按钮 在默认情况下,单选按钮显示为一个圆形图标,可以在图标旁放一些说明文字.通常情况下RadioButton组件需要与RadioGroup组件一起使用,组成一个单选按钮组.RadioGroup是可以容纳多个RadioButton的容器. <LinearLayout     android:layout_width="match_parent"     android:layout_height

  • Android编程实现带有单选按钮和复选按钮的dialog功能示例

    本文实例讲述了Android编程实现带有单选按钮和复选按钮的dialog.分享给大家供大家参考,具体如下: 带有单选按钮的dialog: package example.com.myapplication; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.content.DialogInterface; import android.os.

  • Android ListView ImageView实现单选按钮实例

    做Android开发两年的时间,技术稍稍有一些提升,刚好把自己实现的功能写出来,记录一下,如果能帮助到同行的其他人,我也算是做了件好事,哈哈!!废话不多说,先上个图. 先上一段代码: 1 if (lastposition == position){ 2 viewHolder.setImageResource(R.id.iv_yuandian1,R.mipmap.ic_button_checked); 3 } else { 4 viewHolder.setImageResource(R.id.iv

  • Android studio实现单选按钮

    本文实例为大家分享了Android studio实现单选按钮的具体代码,供大家参考,具体内容如下 创建空activity编辑activity_main.xml文件 代码如下: <?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.LinearLayoutCompat     xmlns:android="http://schemas.android.com/ap

  • android studio实现简单考试应用程序实例代码详解

    一.问题 1.如图所示,设计一个包含四种题型的简单考试应用程序(具体考试题目可以选用以下设计,也可以自己另外确定),项目名称:zuoye06_666 :(666,改成自己的实际编号). 2.布局管理器任选(约束布局相对容易实现). 3."提交"按钮的Text通过字符串资源赋值,不要直接输入"提交"两个字. 4.每题按25分计算,编写相应的程序,答题完成后单击"提交"按钮,在"总得分:"右边文本框中显示实际得分:同时,显示一个T

  • Android studio实现画板功能

    简单概述 在日常生活中,我们经常会突发一些奇思妙想,或是一个画面,或是几个符号.这时候无法使用拍照或者打字功能实现,想拿笔记下又身边找不到笔.于是我琢磨能不能做一个手机端的画板. 效果图 实现过程 项目布局很简单 让我们来看代码:首先声明画笔,画板,和坐标 public class MainActivity extends AppCompatActivity{ Paint paint; Canvas canvas; ImageView imageview; Bitmap bitmap,newbi

  • Android Studio实现简易计算器设计

    本文实例为大家分享了Android Studio实现简易计算器的具体代码,供大家参考,具体内容如下 一.题目 1.如图所示(实际设计,类似此界面样式即可,全屏时,按钮将会纵向拉伸),利用网格布局管理器设计一个居中.满屏计算器,项目名称:clc666b:(666,改成自己的实际编号) 2.加.乘分别用2个单选按钮进行选择: 3.为clc666b编写程序(clc666a不需要编程,只设计界面即可),根据选择的加(乘)单选按钮,实现两个数的加法和乘法的简单计算. 4.为了简化程序设计,上方的数据区也可

  • Android studio实现简易的计算器功能

    本文实例为大家分享了android studio简易运算器,供大家参考,具体内容如下 JAVA语句代码块: package com.example.douyingming; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import android.widget.EditText; import android.widget.Radi

  • 浅析Android Studio 3.0 升级各种坑(推荐)

    点击 Check for Updates 增量更新: 下载完成,会提示更新 您将 Gradle 更新至 4.1: 这里建议您对老项目先暂时点击 Don't remind me on this project,以防有坑.当然我不入地狱谁入地狱,我点 Update,于是问题来了,一直处于下载中,不过,莫担心,我下载好了,公众号聊天界面回复「 gradle-4.1-all 」,下载 gradle-4.1-all.zip 文件,放到: 重启 Android Studio. gradle 目录: Mac系

  • Android Studio 引入 aidl 文件的方法汇总

    AndroidStudio 引入 aidl 文件,一般来说,有两种方法. 第一种方法 直接在 src/main 目录下新建 aidl 文件夹,并将我们的 aidl 文件放到该目录下.因为 AndroidStudio 默认的 aidl 文件默认配置是这样的. 第二种方法 把 adil 文件拷贝到libs文件夹下在build.gradle文件中配置 sourceSets { main { jniLibs.srcDirs = ['libs'] aidl.srcDirs = ['src/main/jav

  • 详解Android Studio 3.0的新特性与适配

    简介 Android Studio升级到3.0后,有不少的改动和新特性,先贴出官方的迁移说明. 本文会持续收集与总结本人在使用Android Studio 3.0进行开发的过程中所遇到的问题. 版本配置 Gradle版本 Android Studio 3.0需要的Gradle版本至少为4.1. 如果是使用gradle wrapper,则工程根目录/gradle/wrapper/gradle-wrapper.properties中的distributionUrl字段为https\://servic

  • android studio 3.0 升级 项目遇到的问题及更改思路(问题小结)

    Android Studio从3.0版本新增了许多功能,当然首当其冲就是从3.0版本新增了对 Kotlin 开发语言的支持,除此之外还有其他一些新功能,例如:Android Profiler (其中包含了: CPU Profiler.Memory Profiler.Network Profiler ),APK Debugger,Device File Explorer,Java 8 Language Features等. android studio 3.0版本升级问题修改: ===> 问题一

  • Android Studio配置Kotlin开发环境详细步骤

    Android Studio配置Kotlin开发环境详细步骤 第一步:安装Kotlin插件 打开Settings面板,找到Plugins选项,点击Browse repositories(浏览仓库),输入"Kotlin"查找,然后安装即可.安装完成之后需要重启Android Studio (切记!). 安装完成之后如下图所示. 插件当前的最新版本是1.1.2-release-Studio-2.3-3. 第二步:配置Kotlin开发环境 点击菜单栏的"Tools"选项,

随机推荐