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

Android單選按鈕RadioButton的使用方法

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

單選按鈕要在一組中選擇一項,并且不能多選。

同一組RadioButton要放在同一個RadioGroup節(jié)點下。

RadioButton默認未選中,點擊后選中但是再次點擊不會取消選中。

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

復(fù)現(xiàn)代碼時出現(xiàn)了一個錯誤,處理單選按鈕的響應(yīng),要先寫一個單選監(jiān)聽器實現(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; // 聲明一個文本視圖對象
 
    @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)聽器,一旦用戶點擊組內(nèi)的單選按鈕,就觸發(fā)監(jiān)聽器的onCheckedChanged方法
        rg_marry.setOnCheckedChangeListener(this);
    }
 
    // 在用戶點擊組內(nèi)的單選按鈕時觸發(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="請選擇您的婚姻狀況"
        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屬性修改單選按鈕的圖標 -->
        <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屬性修改單選按鈕的圖標 -->
        <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

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

相關(guān)文章

  • Android畢業(yè)設(shè)計備忘錄APP

    Android畢業(yè)設(shè)計備忘錄APP

    這篇文章主要介紹了一個Android畢業(yè)設(shè)計備忘錄APP,它很小,但是功能很全,可實現(xiàn)添加、刪除、修改、查看的功能,使用Java語言開發(fā),風(fēng)格簡練
    2021-08-08
  • Android開發(fā)中PopupWindow用法實例分析

    Android開發(fā)中PopupWindow用法實例分析

    這篇文章主要介紹了Android開發(fā)中PopupWindow用法,結(jié)合實例形式分析了PopupWindow彈出窗口效果的使用技巧,需要的朋友可以參考下
    2016-02-02
  • 關(guān)于android studio升級4.1 某些插件使用不了的問題(Mac)

    關(guān)于android studio升級4.1 某些插件使用不了的問題(Mac)

    這篇文章主要介紹了關(guān)于android studio升級4.1 某些插件使用不了的問題(Mac),本文給大家分享解決方法供大家參考,感興趣的朋友跟隨小編一起看看吧
    2020-10-10
  • Android顯示網(wǎng)絡(luò)圖片實例

    Android顯示網(wǎng)絡(luò)圖片實例

    這篇文章主要介紹了Android顯示網(wǎng)絡(luò)圖片的方法,以實例形式展示了Android程序顯示網(wǎng)絡(luò)圖片的方法,非常具有實用價值,需要的朋友可以參考下
    2014-10-10
  • android實現(xiàn)打地鼠游戲

    android實現(xiàn)打地鼠游戲

    這篇文章主要為大家詳細介紹了android實現(xiàn)打地鼠游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • Android仿京東頂部搜索框滑動伸縮動畫效果

    Android仿京東頂部搜索框滑動伸縮動畫效果

    這篇文章主要為大家詳細介紹了Android仿京東頂部搜索框滑動伸縮動畫效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • android Handler詳細使用方法實例

    android Handler詳細使用方法實例

    本文主要介紹Android中Handler的簡單使用方法,Handler跟多線程,消息隊列聯(lián)系很緊密,在平常的實際程序開發(fā)中比較常見。本文分為4個簡單的例子來學(xué)校handler
    2013-11-11
  • 詳解Android中Service AIDL的使用

    詳解Android中Service AIDL的使用

    作為一名Android開發(fā)人員,如果沒聽過Service,那就有點說不過去了啊,Service是Android四大組件之一,它是不依賴于用戶界面的,就是因為Service不依賴與用戶界面,所以我們常常用于進行一些耗時的操作,比如:下載數(shù)據(jù)等;本文將詳細介紹Android中Service AIDL的使用。
    2021-06-06
  • Android使用httpPost向服務(wù)器發(fā)送請求的方法

    Android使用httpPost向服務(wù)器發(fā)送請求的方法

    這篇文章主要介紹了Android使用httpPost向服務(wù)器發(fā)送請求的方法,實例分析了Android針對HttpPost類的操作技巧,需要的朋友可以參考下
    2015-12-12
  • Android在fragment中編寫toobar的步驟詳解

    Android在fragment中編寫toobar的步驟詳解

    這篇文章主要介紹了Android在fragment中編寫toobar,本文分步驟通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-01-01

最新評論