Android基礎(chǔ)控件RadioGroup使用方法詳解
本文為大家分享了Android基礎(chǔ)控件RadioGroup的使用,供大家參考,具體內(nèi)容如下
1.簡單介紹
RadioGroup可以提供幾個(gè)選項(xiàng)供用戶選擇,但只能選擇其中的一個(gè)。其下面可以橫著或者豎著掛幾個(gè)RadioButton,也可以掛載其他控件(如TextView)。RadioGroup的相應(yīng)事件一般不由下面的RadioButton響應(yīng),而是直接由RadioGroup響應(yīng)。實(shí)現(xiàn)RadioGroup.OnCheckedChangeListener接口即可監(jiān)聽RadioGroup。RadioButton也是派生自CompoundButton,也可以通過修改button屬性來修改圖標(biāo),但是通過button屬性修改往往會(huì)使文字和圖標(biāo)挨得很近。這時(shí)候我們可以設(shè)置RadioButton的drawableLeft屬性和drawablePadding屬性來使圖標(biāo)和文字挨得遠(yuǎn)一點(diǎn)(同時(shí)把button屬性設(shè)置成@null)。下圖是RadioGroup的使用效果。
2.簡單使用
下面是RadioGroup的簡單實(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();
}
});
}
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:Android
下一篇:Android采用消息推送實(shí)現(xiàn)類似微信視頻接聽
本文標(biāo)題:Android基礎(chǔ)控件RadioGroup使用方法詳解
本文地址:http://www.jygsgssxh.com/a1/Android/9079.html
您可能感興趣的文章
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方法詳解
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)登錄
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代碼
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10Emoji表情在Android JNI中的兼容性問題詳解
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10android開發(fā)環(huán)境中SDK文件夾下的所需內(nèi)容詳解
- 01-10android異步消息機(jī)制 源碼層面徹底解析(1)


閱讀排行
本欄相關(guān)
- 01-10Android自定義View之繪制圓形頭像功能
- 01-10Android實(shí)現(xiàn)雙擊返回鍵退出應(yīng)用實(shí)現(xiàn)方
- 01-10android實(shí)現(xiàn)簡單計(jì)算器功能
- 01-10android實(shí)現(xiàn)記住用戶名和密碼以及自動(dòng)
- 01-10C++自定義API函數(shù)實(shí)現(xiàn)大數(shù)相乘算法
- 01-10Android 友盟第三方登錄與分享的實(shí)現(xiàn)代
- 01-10android實(shí)現(xiàn)指紋識(shí)別功能
- 01-10如何給Flutter界面切換實(shí)現(xiàn)點(diǎn)特效
- 01-10Android實(shí)現(xiàn)圓形漸變加載進(jìn)度條
- 01-10Emoji表情在Android JNI中的兼容性問題詳
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)


