单选框 RadioButton

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

    <RadioGroup
        android:id="@+id/rdg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RadioButton
            android:id="@+id/rbt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25dp"
            android:text="男"
            />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25dp"
            android:text="女"
            />
    </RadioGroup>

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25dp"
            />

</LinearLayout>

MainActivity.java

package com.iang.buttonclick;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity  {

    private RadioGroup radioGroup;
    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

       radioGroup= (RadioGroup)findViewById(R.id.rdg);
        textView=(TextView) findViewById(R.id.tv);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId==R.id.rbt)
                    textView.setText("您的性别是:男");
                else
                    textView.setText("您的性别是:女");
            }
        });

    }
}

原文地址:https://www.cnblogs.com/PerZhu/p/11599614.html

时间: 2024-08-30 16:44:17

单选框 RadioButton的相关文章

WPF中单选框RadioButton

单选框RadioButton的基本使用: <StackPanel Margin="10"> <Label FontWeight="Bold">Are you ready?</Label> <RadioButton>Yes</RadioButton> <RadioButton>No</RadioButton> <RadioButton IsChecked="True&q

可分组的选择框控件(MVVM下)(Toggle样式 仿造单选框RadioButton,复选框CheckBox功能)

原地址: http://www.cnblogs.com/yk250/p/5660340.html 效果图如下:支持分组的单选框,复选框样式和MVVM下功能的实现.这是项目中一个快捷键功能的扩展. 1,准备工作:VS2015 (15对WPF的支持变得异常的好,调试模式下允许自动更改属性.),随VS发布的Blend,几个基础类: 1 public class RelayCommand : ICommand 2 { 3 #region Fields 4 5 readonly Action<object

Android中单选框RadioButton的基本用法

总结一下设置图标的三种方式: (1)button属性:主要用于图标大小要求不高,间隔要求也不高的场合. (2)background属性:主要用于能够以较大空间显示图标的场合. (3)drawableLeft属性:主要用于对图标与文字之间的间隔有要求的场合. 注意使用 background 或者 drawableLeft时 要设置 android:button="@null" 监听: radioGroup.setOnCheckedChangeListener(new RadioGroup

android 单选框和复选框

<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_heig

Tkinter 之RadioButton单选框标签

一.参数说明 语法 作用 Radiobutton(root,text='xxxx') 单选框文本显示内容 Radiobutton(root,variable=color) 单选框索引变量,通过变量的值确定哪个单选框被选中 Radiobutton(root,variable=color,value='red') 单选框选中时设定变量的值 Radiobutton(root,variable=color,value='red',command=函数) 单选框选中时执行的命令(函数) Radiobutt

单选框与复选框的实现

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.hanqi.test5"> <application android:allowBackup="true" android:icon=&q

android开发入门之单选框的基本应用

随着android手机越来越火,android操作系统也深受开发者的喜爱,很多程序员慢慢转行做android开发.今天麦子学院的android开发老师给大家讲讲android单选框基本应用,今天我们跟着麦子学院android老师学习Android单选框的实现方式. ●使用Android单选框的时候,要用RadioGroup来分组 ●选择RadioGroup里的单选框的时候,将调用RadioGroup的Check·()方法 ●追加单选框被选择时处理的时候, 1.调用setOnCheckedChan

20151220学习内容:Web之单选框、复选框

单选框:RadioButton RadioButton中有个属性GroupName,当单选时,必须把各个选项的组名设成同一个 查看按钮是否选中:bool sex = RadioButton1.Checked; Label1.Text = sex.ToString(); 单选按钮列表:RadioButtonList 属性:RepeatDirection:横向或纵向 绑定数据: TextDataContext context = new TextDataContext(); RadioButtonL

Android初级教程小案例之单选框RadioGroup与复选框CheckBox

Android里面的单选框和html中的其实是一样的效果.这里用到两个控件:CheckBox和RadioGroup.直接上代码: radio.xml布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation=&quo