Android單選按鈕RadioButton的使用詳解
RadioButton是最普通的UI組件之一,繼承了Button類,可以直接使用Button支持的各種屬性和方法。
RadioButton與普通按鈕不同的是,它多了一個(gè)可以選中的功能,可額外指定一個(gè)android:checked屬性,該屬性可以指定初始狀態(tài)時(shí)是否被選中,其實(shí)也可以不用指定,默認(rèn)初始狀態(tài)都不選中。
使用RadioButton必須和單選框RadioGroup一起使用,在RadioGroup中放置RadioButton,通過setOnCheckedChangeListener( )來響應(yīng)按鈕的事件;
(1)選用radioGroup的圖標(biāo)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="44dp"
android:text="性別:"
android:textSize="20dp" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView1"
android:layout_marginLeft="21dp"
android:layout_toRightOf="@+id/textView1"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:onClick="onRadioButtonClicked"
android:text="男" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="女" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClicked"
android:text="保密" />
</RadioGroup>
</RelativeLayout>
(2)控制的類是
package com.lc.radiobutton;
import com.example.radiobutton.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/*
* 設(shè)置radio的點(diǎn)擊事件,當(dāng)點(diǎn)擊的時(shí)候顯示文字
*/
public void onRadioButtonClicked(View view) {
RadioButton button = (RadioButton) view;
boolean isChecked = button.isChecked();
switch (view.getId()) {
case R.id.radio0:
if (isChecked) {
Toast.makeText(MainActivity.this, button.getText(), 1).show();
}
break;
case R.id.radio1:
if (isChecked) {
Toast.makeText(MainActivity.this, button.getText(), 1).show();
}
break;
case R.id.radio2:
if (isChecked) {
Toast.makeText(MainActivity.this, button.getText(), 1).show();
}
break;
default:
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
(3)顯示結(jié)果,當(dāng)點(diǎn)擊的時(shí)候顯示文字

總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Android RxJava異步數(shù)據(jù)處理庫(kù)使用詳解
RxJava是一種異步數(shù)據(jù)處理庫(kù),也是一種擴(kuò)展的觀察者模式。對(duì)于Android開發(fā)者來說,使用RxJava時(shí)也會(huì)搭配RxAndroid,它是RxJava針對(duì)Android平臺(tái)的一個(gè)擴(kuò)展,用于Android 開發(fā),它提供了響應(yīng)式擴(kuò)展組件,使用RxAndroid的調(diào)度器可以解決Android多線程問題2022-11-11
Android開發(fā)技巧之ViewStub控件惰性裝載
布局文件中的控件并不一定在程序啟動(dòng)時(shí)全都用到,有一些控件只在特定的情況下才會(huì)被使用到;我們急需一種機(jī)制來改變<include>標(biāo)簽的這種行為,只在需要時(shí)裝載控件。這種機(jī)制就是本節(jié)要介紹的ViewStub控件2013-01-01
Android?Compose狀態(tài)改變動(dòng)畫animateXxxAsState使用詳解
這篇文章主要為大家介紹了Android?Compose狀態(tài)改變動(dòng)畫animateXxxAsState使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Android實(shí)現(xiàn)搖一搖簡(jiǎn)單功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)搖一搖簡(jiǎn)單功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
android開發(fā)實(shí)踐之ndk編譯命令簡(jiǎn)單示例
這篇文章主要給大家介紹了在android中ndk編譯命令使用的相關(guān)資料,文中詳細(xì)介紹了ndk-build命令行參數(shù),并通過簡(jiǎn)單的示例代碼給大家介紹了如何編寫 .c 文件,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06
Android SurfaceView與TextureView使用方法詳細(xì)講解
SurfaceView和TextureView都繼承View,與普通的View不同的是,它倆可以在獨(dú)立線程中繪制渲染,性能更高,所以常被應(yīng)用在對(duì)繪制速率要求比較高的場(chǎng)景,比如相機(jī)預(yù)覽,視頻播放等等2022-10-10

