zoukankan      html  css  js  c++  java
  • 一手遮天 Android

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - view(选择类): RadioButton 样式

    示例如下:

    /view/selection/RadioButtonDemo2.java

    /**
     * RadioGroup - 单选框组
     * RadioButton - 单选框
     *     setButtonDrawable() - 设置单选框图片在各种状态下的样式
     *
     *
     * 本例主要介绍 RadioButton 的样式
     */
    
    package com.webabcd.androiddemo.view.selection;
    
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.RadioButton;
    
    import com.webabcd.androiddemo.R;
    
    public class RadioButtonDemo2 extends AppCompatActivity {
    
        private RadioButton _radioButton3;
        private RadioButton _radioButton4;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_view_selection_radiobuttondemo2);
    
            _radioButton3 = (RadioButton)findViewById(R.id.radioButton3);
            _radioButton4 = (RadioButton)findViewById(R.id.radioButton4);
    
            sample();
        }
    
        private void sample() {
            _radioButton3.setButtonDrawable(R.drawable.selector_radiobutton_button);
            _radioButton4.setButtonDrawable(R.drawable.selector_radiobutton_button);
        }
    }
    
    

    /layout/activity_view_selection_radiobuttondemo2.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 - 单选框组(要声明 id 否则会有问题)
            RadioButton - 单选框(要声明 id 否则会有问题)
                button - 设置单选框图片在各种状态下的样式(参见 drawable/selector_radiobutton_button.xml)
        -->
    
        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
    
            <RadioButton
                android:id="@+id/radioButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@drawable/selector_radiobutton_button"
                android:checked="true"
                android:text="radioButton1" />
    
            <RadioButton
                android:id="@+id/radioButton2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:button="@drawable/selector_radiobutton_button"
                android:text="radioButton2" />
        </RadioGroup>
    
    
        <!--
            在 java 中设置 RadioButton 的 button
            调整单选框的图标与文字的间距可以通过 padding 来实现
        -->
        <RadioGroup
            android:id="@+id/radioGroup2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">
    
            <RadioButton
                android:id="@+id/radioButton3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:paddingLeft="10dp"
                android:paddingTop="10dp"
                android:text="radioButton3" />
    
            <RadioButton
                android:id="@+id/radioButton4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="10dp"
                android:paddingTop="10dp"
                android:text="radioButton4" />
        </RadioGroup>
    
    </LinearLayout>
    
    

    /drawable/selector_radiobutton_button.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
        <!--
            state_enabled="true" - 可用
            state_enabled="false" - 不可用
            state_checked="true" - 选中
            state_checked="false" - 未选中
    
            注意:可以直接在 item 的 drawable 指定图片,但是这样只能按照原图的大小显示,如果需要指定图片的显示大小的话,则需要像本例这样使用 layer-list
        -->
    
        <item
            android:state_enabled="true"
            android:state_checked="true"
            android:drawable="@drawable/layerlist_radiobutton_button_checked" />
        <item
            android:state_enabled="true"
            android:state_checked="false"
            android:drawable="@drawable/layerlist_radiobutton_button_unchecked" />
    </selector>
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    hdu 4651 Partition (利用五边形定理求解切割数)
    单点登录SSO的实现原理
    高速排序算法
    2014 百度之星第三题
    TR069协议向导——一个帮助你了解TR069协议的简明教程(一)
    教你用笔记本破解无线路由器password
    人脸识别算法初次了解
    JSP验证码
    GROUP BY,WHERE,HAVING之间的差别和使用方法
    typedef函数指针使用方法
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_view_selection_RadioButtonDemo2.html
Copyright © 2011-2022 走看看