最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android單選按鈕RadioButton的使用方法

 更新時(shí)間:2021年05月27日 11:29:59   作者:打代碼的浪浪  
這篇文章主要為大家詳細(xì)介紹了Android單選按鈕RadioButton的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

單選按鈕要在一組中選擇一項(xiàng),并且不能多選。

同一組RadioButton要放在同一個(gè)RadioGroup節(jié)點(diǎn)下。

RadioButton默認(rèn)未選中,點(diǎn)擊后選中但是再次點(diǎn)擊不會(huì)取消選中。

RadioButton經(jīng)常會(huì)更換按鈕圖標(biāo),如果通過button屬性變更圖標(biāo),那么圖標(biāo)與文字就會(huì)挨得很近。為了拉開圖標(biāo)與文字之間的距離,得換成drawableLeft屬性展示新圖標(biāo)(不要忘記把button改為@null),再設(shè)置drawablePadding即可指定間隔距離。

復(fù)現(xiàn)代碼時(shí)出現(xiàn)了一個(gè)錯(cuò)誤,處理單選按鈕的響應(yīng),要先寫一個(gè)單選監(jiān)聽器實(shí)現(xiàn)接口 RadioGroup.OnCheckedChangeListener,而不是復(fù)合按鈕的CompoundButton.OnCheckedChangeListener。

MainActivity

package com.example.middle;
 
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;
 
public class RadioVerticalActivity extends AppCompatActivity implements OnCheckedChangeListener {
    private TextView tv_marry; // 聲明一個(gè)文本視圖對(duì)象
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_vertical);
        // 從布局文件中獲取名叫tv_marry的文本視圖
        tv_marry = findViewById(R.id.tv_marry);
        // 從布局文件中獲取名叫rg_marry的單選組
        RadioGroup rg_marry = findViewById(R.id.rg_marry);
        // 給rg_marry設(shè)置單選監(jiān)聽器,一旦用戶點(diǎn)擊組內(nèi)的單選按鈕,就觸發(fā)監(jiān)聽器的onCheckedChanged方法
        rg_marry.setOnCheckedChangeListener(this);
    }
 
    // 在用戶點(diǎn)擊組內(nèi)的單選按鈕時(shí)觸發(fā)
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if (checkedId == R.id.rb_married) {
            tv_marry.setText("哇哦,祝你早生貴子");
        } else if (checkedId == R.id.rb_unmarried) {
            tv_marry.setText("哇哦,你的前途不可限量");
        }
    }
 
}

Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >
 
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="請(qǐng)選擇您的婚姻狀況"
        android:textColor="#000000"
        android:textSize="17sp" />
 
    <RadioGroup
        android:id="@+id/rg_marry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
 
        <!-- 通過button屬性修改單選按鈕的圖標(biāo) -->
        <RadioButton
            android:id="@+id/rb_unmarried"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:button="@drawable/radio_selector"
            android:text="未婚"
            android:textColor="#000000"
            android:textSize="17sp" />
 
        <!-- 通過drawableLeft屬性修改單選按鈕的圖標(biāo) -->
        <RadioButton
            android:id="@+id/rb_married"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:button="@null"
            android:drawableLeft="@drawable/radio_selector"
            android:drawablePadding="10dp"
            android:text="已婚"
            android:textColor="#000000"
            android:textSize="17sp" />
    </RadioGroup>
 
    <TextView
        android:id="@+id/tv_marry"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:textSize="17sp" />
</LinearLayout>

result

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • android canvas drawText()文字居中效果

    android canvas drawText()文字居中效果

    這篇文章主要為大家詳細(xì)介紹了android canvas drawText()文字居中效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android實(shí)現(xiàn)仿美團(tuán)、順豐快遞數(shù)據(jù)加載效果

    Android實(shí)現(xiàn)仿美團(tuán)、順豐快遞數(shù)據(jù)加載效果

    本片文章教給大家用Android實(shí)現(xiàn)美團(tuán)和順豐快遞APP的數(shù)據(jù)加載的動(dòng)畫效果,有興趣的朋友跟著學(xué)習(xí)嘗試下吧。
    2017-12-12
  • Android實(shí)例代碼理解設(shè)計(jì)模式SOLID六大原則

    Android實(shí)例代碼理解設(shè)計(jì)模式SOLID六大原則

    程序設(shè)計(jì)領(lǐng)域, SOLID (單一功能、開閉原則、里氏替換、接口隔離以及依賴反轉(zhuǎn))是由羅伯特·C·馬丁在21世紀(jì)早期 引入的記憶術(shù)首字母縮略字,指代了面向?qū)ο缶幊毯兔嫦驅(qū)ο笤O(shè)計(jì)的基本原則
    2021-10-10
  • Android中的異步任務(wù)處理與UI更新技巧

    Android中的異步任務(wù)處理與UI更新技巧

    在Android開發(fā)中,異步任務(wù)處理和UI更新是兩個(gè)非常重要的概念,本文將詳細(xì)介紹Android中異步任務(wù)的處理方式以及如何在異步任務(wù)完成后更新UI,并通過代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下
    2024-09-09
  • 詳解Glide最新版V4使用指南

    詳解Glide最新版V4使用指南

    這篇文章主要介紹了詳解Glide最新版V4使用指南,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • Android自定義ActionProvider ToolBar實(shí)現(xiàn)Menu小紅點(diǎn)

    Android自定義ActionProvider ToolBar實(shí)現(xiàn)Menu小紅點(diǎn)

    這篇文章主要介紹了Android自定義ActionProvider ToolBar實(shí)現(xiàn)Menu小紅點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android中Handler與Message的簡(jiǎn)單實(shí)例

    Android中Handler與Message的簡(jiǎn)單實(shí)例

    這篇文章主要介紹了Android中Handler與Message的簡(jiǎn)單實(shí)例的相關(guān)資料,這里提供實(shí)例來說明線程Handler與message 的結(jié)合使用,需要的朋友可以參考下
    2017-08-08
  • 在Android項(xiàng)目中使用AspectJ的詳細(xì)攻詻

    在Android項(xiàng)目中使用AspectJ的詳細(xì)攻詻

    AspectJ是實(shí)現(xiàn)AOP的其中一款框架,內(nèi)部通過處理字節(jié)碼實(shí)現(xiàn)代碼注入,文章給大家提到AspectJ基礎(chǔ)語法和集成AspectJ的方式,對(duì)AspectJ在android中使用教程感興趣的朋友跟隨小編一起看看吧
    2021-06-06
  • 詳解Android .9.png “點(diǎn)九”圖片的使用

    詳解Android .9.png “點(diǎn)九”圖片的使用

    這篇文章主要為大家詳細(xì)介紹了Android .9.png “點(diǎn)九”圖片的使用方法,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android四大組件之Activity詳細(xì)介紹

    Android四大組件之Activity詳細(xì)介紹

    大家好,本篇文章主要講的是Android四大組件之Activity詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12

最新評(píng)論

禹城市| 临邑县| 嘉荫县| 巴楚县| 米脂县| 滦南县| 永州市| 嘉定区| 丰台区| 佛坪县| 阜宁县| 旬邑县| 湖南省| 朝阳区| 木里| 东海县| 巧家县| 秭归县| 称多县| 林口县| 德化县| 延津县| 名山县| 清水河县| 华宁县| 克山县| 五寨县| 灵丘县| 隆林| 讷河市| 隆子县| 蓬安县| 辽中县| 南和县| 巴中市| 化德县| 华安县| 封开县| 宝清县| 清流县| 云南省|