Android單選按鈕RadioButton的使用詳解
RadioButton是最普通的UI組件之一,繼承了Button類,可以直接使用Button支持的各種屬性和方法。
RadioButton與普通按鈕不同的是,它多了一個可以選中的功能,可額外指定一個android:checked屬性,該屬性可以指定初始狀態(tài)時是否被選中,其實也可以不用指定,默認初始狀態(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的點擊事件,當(dāng)點擊的時候顯示文字 */ 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)點擊的時候顯示文字
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
詳解AndroidStudio JNI +Gradle3.0以上JNI爬坑之旅
這篇文章主要介紹了詳解AndroidStudio JNI +Gradle3.0以上JNI爬坑之旅,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12Android實現(xiàn)音量調(diào)節(jié)的方法
這篇文章主要介紹了Android實現(xiàn)音量調(diào)節(jié)的方法,涉及Android頁面布局及多媒體播放的設(shè)置技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-09-09Android集成GreenDao數(shù)據(jù)庫的操作步驟
這篇文章主要介紹了Android集成GreenDao數(shù)據(jù)庫,使用數(shù)據(jù)庫存儲時候,一般都會使用一些第三方ORM框架,比如GreenDao,本文分幾步給大家介紹Android集成GreenDao數(shù)據(jù)庫的方法,需要的朋友可以參考下2022-10-10Flutter實現(xiàn)底部導(dǎo)航欄創(chuàng)建詳解
ConvexBottomBar是一個底部導(dǎo)航欄組件,用于展現(xiàn)凸起的TAB效果,支持多種內(nèi)置樣式與動畫交互。本文將利用ConvexBottomBar創(chuàng)建漂亮的底部導(dǎo)航欄,感興趣的可以學(xué)習(xí)一下2022-01-01Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式
本篇文章主要介紹了Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式,非常具有實用價值,需要的朋友可以參考下2017-09-09Android 接收微信、QQ其他應(yīng)用打開第三方分享功能
這篇文章主要介紹了Android 接收微信、QQ其他應(yīng)用打開,第三方分享 ,思路很簡單通過在AndroidManifest.xml注冊ACTION事件,在用于接收分享的Activity里面加接收代碼,感興趣的朋友可以一起學(xué)習(xí)下2022-11-11Android利用Flutter?path繪制粽子的示例代碼
端午將至,作為中華民族的非常重要的傳統(tǒng)節(jié)日,粽子那是必不可少的。今天跟隨本篇文章用Flutter?path畫一個會科普節(jié)日的的粽子吧2022-05-05