http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2203|回复: 4

[新手开发之旅] Android新手开发之旅-RadioGroup和RadioButton的简单用法

[复制链接]
发表于 2018-12-6 16:36:10 | 显示全部楼层 |阅读模式
本帖最后由 liu 于 2018-12-6 15:38 编辑
RadioGroup和RadioButton的简单用法




Android新手开发之旅【目录】





RadioGroup是单选组合框,可以容纳多个RadioButton,RadioGroup中包含的多个RadioButton只能选择一个


RadioGroup常用属性:

RadioGroup.getCheckedRadioButtonId()          获取选中按钮的id
RadioGroup.clearCheck()                                清除选中状态
RadioButton.getText()                           获取单选框的值
RadioGroup.setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListener listener)      监听事件





示例代码:

activity_main:
[XML] 纯文本查看 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选择城市:"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="10dp">

        <RadioButton
            android:id="@+id/radiobutton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="开封" />

        <RadioButton
            android:id="@+id/radiobutton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="郑州" />

        <RadioButton
            android:id="@+id/radiobutton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="北京" />
    </RadioGroup>

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="你选择的是:"
        android:textColor="@android:color/black"
        android:textSize="18sp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="清除选中状态"
        android:textColor="@android:color/black" />
</LinearLayout>

java代码:
[Java] 纯文本查看 复制代码
package com.company.helloworld.firstapplication;

import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private RadioGroup mRadioGroup;
    private RadioButton mRadioButton1;
    private RadioButton mRadioButton2;
    private RadioButton mRadioButton3;
    private TextView mTextView;
    private Button mButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mRadioGroup = (RadioGroup) findViewById(R.id.radiogroup);
        mRadioButton1 = (RadioButton) findViewById(R.id.radiobutton1);
        mRadioButton2 = (RadioButton) findViewById(R.id.radiobutton2);
        mRadioButton3 = (RadioButton) findViewById(R.id.radiobutton3);
        mTextView = (TextView) findViewById(R.id.textview);
        mButton = (Button) findViewById(R.id.button);

        //RadioGroup监听事件
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                //radioGroup.getCheckedRadioButtonId()获取状态改变的RadioButton的id
                switch (radioGroup.getCheckedRadioButtonId()) {
                    case R.id.radiobutton1:
                        mTextView.setText("你选择的是: " + mRadioButton1.getText().toString());
                        break;
                    case R.id.radiobutton2:
                        mTextView.setText("你选择的是: " + mRadioButton2.getText().toString());
                        break;
                    case R.id.radiobutton3:
                        mTextView.setText("你选择的是: " + mRadioButton3.getText().toString());
                        break;
                }
            }
        });

        //清除选中状态
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mRadioGroup.clearCheck();
            }
        });
    }
}


效果如下图:

x.gif




















1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2018-12-6 17:35:30 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-12-6 17:37:49 | 显示全部楼层
我只是路过打酱油的。
发表于 2018-12-6 17:38:12 | 显示全部楼层
支持一下楼主,加油!
发表于 2018-12-7 17:24:48 | 显示全部楼层
强烈支持楼主ing……
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-20 18:10

© 2014-2021

快速回复 返回顶部 返回列表