zoukankan      html  css  js  c++  java
  • Android随笔

    RadioButton(单选按钮)

    如题单选按钮,就是只能够选中一个,所以我们需要把RadioButton放到RadioGroup按钮组中,从而实现 单选功能!先熟悉下如何使用RadioButton,一个简单的性别选择的例子: 另外我们可以为外层RadioGroup设置orientation属性然后设置RadioButton的排列方式,是竖直还是水平~

    效果图:

    PS:笔者的手机是Android 5.0.1的,这里的RadioButton相比起旧版本的RadioButton,稍微好看一点~

    布局代码如下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="请选择性别"
            android:textSize="23dp"
            />
    
        <RadioGroup
            android:id="@+id/radioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <RadioButton
                android:id="@+id/btnMan"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男"
                android:checked="true"/>
    
            <RadioButton
                android:id="@+id/btnWoman"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女"/>
        </RadioGroup>
    
        <Button
            android:id="@+id/btnpost"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="提交"/>
    
    </LinearLayout>

    获得选中的值:

    这里有两种方法,

    第一种是为RadioButton设置一个事件监听器setOnCheckChangeListener

    例子代码如下:

    RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);
            //第一种获得单选按钮值的方法  
            //为radioGroup设置一个监听器:setOnCheckedChanged()  
            radgroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    RadioButton radbtn = (RadioButton) findViewById(checkedId);
                    Toast.makeText(getApplicationContext(), "按钮组值发生改变,你选了" + radbtn.getText(), Toast.LENGTH_LONG).show();
                }
            });

    运行效果图: 

  • 相关阅读:
    windows下启动数据库、创建数据表、角色等
    直播流的来源
    win10如何查看已保存的账号信息
    Tomcat+IDEA
    线程安全-Spring Bean 作用域类型(Scope)
    ECMAScript6学习-2.1let与const
    解决Mac上Android开发时adb连接不到手机问题
    8个不可不知的Mac OS X专用命令行工具(转)
    ios 中获得应用程序名称和版本号
    iPhone 6 图像渲染揭秘(转)
  • 原文地址:https://www.cnblogs.com/wrx166/p/14909543.html
Copyright © 2011-2022 走看看