欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android控件RadioButton的使用方法

 更新時間:2021年05月12日 10:48:38   作者:緊張的無痕  
這篇文章主要為大家詳細介紹了Android控件RadioButton的使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android控件RadioButton的使用代碼,供大家參考,具體內(nèi)容如下

內(nèi)容

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RadioActivity">
    <RadioGroup //定義一個單選按鈕組
        android:id="@+id/rg_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton //單選按鈕一 使用默認樣式
            android:id="@+id/rb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="男"
            android:textSize="24sp"
            android:textColor="@color/black"/>
        <RadioButton //單選按鈕2 使用默認樣式
            android:id="@+id/rb_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="女"
            android:textSize="24sp"
            android:textColor="@color/black"/>
    </RadioGroup>
    <RadioGroup //組2
        android:id="@+id/rg_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@id/rg_1"
        android:layout_marginTop="50dp">
        <RadioButton
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:text="男"
            android:button="@null" //無按鈕樣式
            android:textSize="24sp"
            android:background="@drawable/selector_radiobutton" //自定義背景
            android:textColor="@color/black"
            android:checked="true"
            android:gravity="center"/>
        <RadioButton
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:button="@null" //無按鈕樣式
            android:text="女"
            android:background="@drawable/selector_radiobutton" //自定義背景
            android:textSize="24sp"
            android:textColor="@color/black"/>
    </RadioGroup>
</RelativeLayout>

//selector_radiobutton.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true"> //單選被選中的樣式
        <shape android:shape="rectangle">
            <solid android:color="#ff66ff"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
    <item android:state_checked="false"> //單選沒被選中的樣式
        <shape android:shape="rectangle">
            <stroke android:color="#cc33ff" android:width="2dp"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
</selector>
public class RadioActivity extends AppCompatActivity {
 
    private RadioGroup rg_1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio);
        rg_1 = findViewById(R.id.rg_1);
        rg_1.setOnCheckedChangeListener((group, checkedId) -> {//設(shè)置組中單選按鈕選中事件
            RadioButton radioButton = findViewById(checkedId);//獲取被選中的id
            Toast.makeText(RadioActivity.this,radioButton.getText(),Toast.LENGTH_SHORT)
                    .show();//吐司一下被選中的文本值
        });
    }
}

運行效果

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

相關(guān)文章

  • 百度語音識別(Baidu Voice) Android studio版本詳解

    百度語音識別(Baidu Voice) Android studio版本詳解

    這篇文章主要介紹了百度語音識別(Baidu Voice) Android studio版本詳解的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • Android實現(xiàn)可使用自定義透明Dialog樣式的Activity完整實例

    Android實現(xiàn)可使用自定義透明Dialog樣式的Activity完整實例

    這篇文章主要介紹了Android實現(xiàn)可使用自定義透明Dialog樣式的Activity,結(jié)合完整實例形式分析了Android Activity自定義style的操作步驟與相關(guān)技巧,需要的朋友可以參考下
    2016-07-07
  • Flow如何解決背壓問題的方法詳解

    Flow如何解決背壓問題的方法詳解

    這篇文章主要為大家介紹了Flow如何解決背壓問題的方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • Flutter實現(xiàn)矩形取色器的封裝

    Flutter實現(xiàn)矩形取色器的封裝

    這篇文章主要為大家詳細介紹了Flutter實現(xiàn)矩形取色器的封裝,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Android開發(fā)實現(xiàn)的內(nèi)存管理工具類

    Android開發(fā)實現(xiàn)的內(nèi)存管理工具類

    這篇文章主要介紹了Android開發(fā)實現(xiàn)的內(nèi)存管理工具類,可實現(xiàn)計算手機內(nèi)部與外部的總存儲空間、可用存儲空間等功能,需要的朋友可以參考下
    2017-11-11
  • Android轉(zhuǎn)場動畫深入分析探究

    Android轉(zhuǎn)場動畫深入分析探究

    對于一個動畫而言,它是由多個分鏡頭組成的,而轉(zhuǎn)場就是分鏡之間銜接方式。轉(zhuǎn)場的主要目的,就是為了讓鏡頭與鏡頭之間過渡的更加自然,讓動畫更加連貫,例如兩個頁面切換之間的銜接動畫
    2022-10-10
  • Android 自定義圓形帶刻度漸變色的進度條樣式實例代碼

    Android 自定義圓形帶刻度漸變色的進度條樣式實例代碼

    這篇文章主要介紹了Android 自定義圓形帶刻度漸變色的進度條的實例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-11-11
  • Android 本地廣播和強制下線功能的實現(xiàn)代碼

    Android 本地廣播和強制下線功能的實現(xiàn)代碼

    這篇文章主要介紹了Android 本地廣播和強制下線功能的實現(xiàn)代碼,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • 一文講解Kotlin中的contract到底有什么用

    一文講解Kotlin中的contract到底有什么用

    我們在開發(fā)中肯定會經(jīng)常用Kotlin提供的一些通用拓展函數(shù),當(dāng)我們進去看源碼的時候會發(fā)現(xiàn)許多函數(shù)里面有contract{}包裹的代碼塊,那么這些代碼塊到底有什么作用呢?下面這篇文章主要給大家介紹了關(guān)于Kotlin中contract到底有什么用的相關(guān)資料,需要的朋友可以參考下
    2022-01-01
  • Android控件RecyclerView實現(xiàn)混排效果仿網(wǎng)易云音樂

    Android控件RecyclerView實現(xiàn)混排效果仿網(wǎng)易云音樂

    這篇文章主要為大家詳細介紹了Android控件RecyclerView實現(xiàn)混排效果,仿網(wǎng)易云音樂,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10

最新評論