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

Android基礎(chǔ)控件RadioGroup使用方法詳解

 更新時(shí)間:2019年11月16日 13:32:03   作者:你的益達(dá)  
這篇文章主要為大家詳細(xì)介紹了Android基礎(chǔ)控件RadioGroup的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文為大家分享了Android基礎(chǔ)控件RadioGroup的使用,供大家參考,具體內(nèi)容如下

1.簡(jiǎn)單介紹

RadioGroup可以提供幾個(gè)選項(xiàng)供用戶(hù)選擇,但只能選擇其中的一個(gè)。其下面可以橫著或者豎著掛幾個(gè)RadioButton,也可以掛載其他控件(如TextView)。RadioGroup的相應(yīng)事件一般不由下面的RadioButton響應(yīng),而是直接由RadioGroup響應(yīng)。實(shí)現(xiàn)RadioGroup.OnCheckedChangeListener接口即可監(jiān)聽(tīng)RadioGroup。RadioButton也是派生自CompoundButton,也可以通過(guò)修改button屬性來(lái)修改圖標(biāo),但是通過(guò)button屬性修改往往會(huì)使文字和圖標(biāo)挨得很近。這時(shí)候我們可以設(shè)置RadioButton的drawableLeft屬性和drawablePadding屬性來(lái)使圖標(biāo)和文字挨得遠(yuǎn)一點(diǎn)(同時(shí)把button屬性設(shè)置成@null)。下圖是RadioGroup的使用效果。

2.簡(jiǎn)單使用

下面是RadioGroup的簡(jiǎn)單實(shí)現(xiàn)代碼。

radio_group_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <!--選中-->
  <item android:state_checked="true" android:drawable="@drawable/radio_choose"/>

  <!--普通狀態(tài)-->
  <item android:drawable="@drawable/radio_unchoose"/>
</selector>

activity_radio_group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".RadioGroupActivity"
  android:orientation="vertical">

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:textColor="#000000"
    android:text="這是橫著放的RadioGroup"/>

  <RadioGroup
    android:id="@+id/rg_horizontal_demo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <RadioButton
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:checked="false"
      android:text="好"
      android:textSize="18sp"
      android:id="@+id/rb_horizontal_good"
      android:textColor="#000000"/>

    <RadioButton
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:checked="false"
      android:text="很好"
      android:textSize="18sp"
      android:id="@+id/rb_horizontal_very_good"
      android:textColor="#000000"/>
  </RadioGroup>

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:textColor="#000000"
    android:text="這是豎著放的RadioGroup"/>

  <RadioGroup
    android:id="@+id/rg_vertical_demo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RadioButton
      android:layout_width="wrap_content"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:checked="false"
      android:text="好"
      android:textSize="18sp"
      android:id="@+id/rb_vertical_good"
      android:textColor="#000000"/>

    <RadioButton
      android:layout_width="wrap_content"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:checked="false"
      android:text="很好"
      android:textSize="18sp"
      android:id="@+id/rb_vertical_very_good"
      android:textColor="#000000"/>
  </RadioGroup>

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp"
    android:textColor="#000000"
    android:text="這是改了圖標(biāo)豎著放的RadioGroup"/>

  <RadioGroup
    android:id="@+id/rg_vertical_custom_demo"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <RadioButton
      android:button="@drawable/radio_button_selector"
      android:layout_width="wrap_content"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:checked="false"
      android:text="這個(gè)是直接設(shè)置button的RadioButton"
      android:textSize="18sp"
      android:id="@+id/rb_vertical_custom_good"
      android:textColor="#000000"/>

    <RadioButton
      android:button="@null"
      android:drawableLeft="@drawable/radio_button_selector"
      android:drawablePadding="10dp"
      android:layout_width="wrap_content"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:checked="false"
      android:text="這個(gè)是設(shè)置drawableLeft屬性的RadioButton"
      android:textSize="18sp"
      android:id="@+id/rb_vertical_custom_very_good"
      android:textColor="#000000"/>
  </RadioGroup>
</LinearLayout>

RadioGroupActivity.java

package xyz.strasae.androidlearn.my;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class RadioGroupActivity extends AppCompatActivity {
  RadioGroup rg_horizontal_demo;
  RadioGroup rg_vertical_demo;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_radio_group);
    rg_horizontal_demo = findViewById(R.id.rg_horizontal_demo);
    rg_vertical_demo = findViewById(R.id.rg_vertical_demo);
    rg_horizontal_demo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup radioGroup, int i) {
        RadioButton rb_temp = findViewById(radioGroup.getCheckedRadioButtonId());
        Toast.makeText(RadioGroupActivity.this, String.format("你選擇了%s", rb_temp.getText().toString()), Toast.LENGTH_SHORT).show();
      }
    });
    rg_vertical_demo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(RadioGroup radioGroup, int i) {
        RadioButton rb_temp = findViewById(radioGroup.getCheckedRadioButtonId());
        Toast.makeText(RadioGroupActivity.this, String.format("你選擇了%s", rb_temp.getText().toString()), Toast.LENGTH_SHORT).show();
      }
    });
  }
}

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

相關(guān)文章

  • android開(kāi)發(fā)仿ios的UIScrollView實(shí)例代碼

    android開(kāi)發(fā)仿ios的UIScrollView實(shí)例代碼

    下面小編就為大家分享一篇android開(kāi)發(fā)仿ios的UIScrollView實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-01-01
  • android操作SQLite增刪改減實(shí)現(xiàn)代碼

    android操作SQLite增刪改減實(shí)現(xiàn)代碼

    android操作SQLite增刪改減實(shí)現(xiàn)代碼,學(xué)習(xí)android的朋友可以參考下。
    2010-11-11
  • Kotlin協(xié)程上下文與上下文元素深入理解

    Kotlin協(xié)程上下文與上下文元素深入理解

    協(xié)程上下文是一個(gè)有索引的Element實(shí)例集合,每個(gè)element在這個(gè)集合里有一個(gè)唯一的key;協(xié)程上下文包含用戶(hù)定義的一些數(shù)據(jù)集合,這些數(shù)據(jù)與協(xié)程密切相關(guān);協(xié)程上下文用于控制線程行為、協(xié)程的生命周期、異常以及調(diào)試
    2022-08-08
  • Android使用動(dòng)畫(huà)動(dòng)態(tài)添加商品進(jìn)購(gòu)物車(chē)

    Android使用動(dòng)畫(huà)動(dòng)態(tài)添加商品進(jìn)購(gòu)物車(chē)

    這篇文章主要為大家詳細(xì)介紹了Android使用動(dòng)畫(huà)動(dòng)態(tài)添加商品進(jìn)購(gòu)物車(chē),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Android ActivityManager使用案例詳解

    Android ActivityManager使用案例詳解

    這篇文章主要介紹了Android ActivityManager使用案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Android項(xiàng)目基本結(jié)構(gòu)詳解

    Android項(xiàng)目基本結(jié)構(gòu)詳解

    這篇文章主要為大家詳細(xì)介紹了Android項(xiàng)目基本結(jié)構(gòu),從最基本的內(nèi)容講起,帶你逐步進(jìn)入用C#進(jìn)行Android應(yīng)用開(kāi)發(fā)的樂(lè)園,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android 3D滑動(dòng)菜單完全解析 Android實(shí)現(xiàn)推拉門(mén)式的立體特效

    Android 3D滑動(dòng)菜單完全解析 Android實(shí)現(xiàn)推拉門(mén)式的立體特效

    這篇文章主要為大家詳細(xì)介紹了Android 3D滑動(dòng)菜單,Android實(shí)現(xiàn)推拉門(mén)式的立體特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • android編程之menu按鍵功能實(shí)現(xiàn)方法

    android編程之menu按鍵功能實(shí)現(xiàn)方法

    這篇文章主要介紹了android編程之menu按鍵功能實(shí)現(xiàn)方法,實(shí)例分析了Android實(shí)現(xiàn)menu的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • android自定義view用path畫(huà)長(zhǎng)方形

    android自定義view用path畫(huà)長(zhǎng)方形

    這篇文章主要為大家詳細(xì)介紹了android自定義view用path畫(huà)長(zhǎng)方形,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 詳解Android Handler的使用

    詳解Android Handler的使用

    這篇文章主要介紹了Android Handler使用的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用Android,感興趣的朋友可以了解下
    2021-04-04

最新評(píng)論