android實(shí)現(xiàn)單選按鈕功能
在我們平時(shí)在注冊(cè)個(gè)人信息的時(shí)候,經(jīng)常會(huì)讓我們選擇是男生還是女生,那么這個(gè)單選框在Android中是怎么實(shí)現(xiàn)的呢?現(xiàn)在我們就來學(xué)習(xí)一下吧
首先我們要明白實(shí)現(xiàn)這樣一個(gè)效果需要哪幾部?
1、在layout布局文件中建立一個(gè)文件,我起的名字為activity_radio.xml
代碼為:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- RadioButton 要想實(shí)現(xiàn)多選一的效果必須放到RadioGroup 中,否則無法實(shí)現(xiàn)多選一的效果. 技巧:要面向RadioGroup 編程,不要面向RaidoButton 編程,否則將增加很大代碼量 android:orientation="vertical":執(zhí)行按鈕組的方向,默認(rèn)值是vertical. RadioGroup的父類時(shí)LinearLayout,但方向的默認(rèn)值不再是線性布局的水平方向了,而是改成了垂直方向. --> <RadioGroup android:id="@+id/radioGroup_gender" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical" > <RadioButton android:id="@+id/radioButton_male" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:checked="true" android:text="男" /> <RadioButton android:id="@+id/radioButton_female" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:checked="false" android:text="女" /> </RadioGroup> </LinearLayout>
2、在MainActivity中實(shí)現(xiàn)細(xì)節(jié)的功能
package com.hsj.example.commoncontroldemo02; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class MainActivity_bak06 extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener { private RadioGroup radioGroup_gender; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_radio); this.radioGroup_gender= (RadioGroup) this.findViewById(R.id.radioGroup_gender); this.radioGroup_gender.setOnCheckedChangeListener(this); } /** * 當(dāng)單選按鈕的狀態(tài)發(fā)生變化時(shí)自動(dòng)調(diào)用的方法 * @param group 單選按鈕所在的按鈕組的對(duì)象 * @param checkedId 用戶選中的單選按鈕的id值 */ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { //得到用戶選中的 RadioButton 對(duì)象 RadioButton radioButton_checked= (RadioButton) group.findViewById(checkedId); String gender=radioButton_checked.getText().toString(); Toast.makeText(this,gender,Toast.LENGTH_LONG).show(); switch (checkedId){ case R.id.radioButton_male: //當(dāng)用戶點(diǎn)擊男性按鈕時(shí)執(zhí)行的代碼 System.out.println("===男性==="); break; case R.id.radioButton_female: //當(dāng)用戶點(diǎn)擊女性按鈕時(shí)執(zhí)行的代碼 System.out.println("===女性==="); break; } System.out.println("===onCheckedChanged(RadioGroup group="+group+", int checkedId="+checkedId+")=="); } }
那么以上就是一個(gè)簡單的單選框的實(shí)現(xiàn),希望對(duì)大家會(huì)有所幫助。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 使用Vitamio打造自己的萬能播放器(6)——在線播放(播放列表)
本文主要介紹Android Vitamino在線播放列表,這里給大家提供效果圖和實(shí)例代碼以便大家參考學(xué)習(xí),希望能幫助開發(fā)Android視頻播放的朋友2016-07-07Android開發(fā)之判斷有無虛擬按鍵(導(dǎo)航欄)的實(shí)例
下面小編就為大家分享一篇Android開發(fā)之判斷有無虛擬按鍵(導(dǎo)航欄)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android實(shí)現(xiàn)光點(diǎn)模糊漸變的自旋轉(zhuǎn)圓環(huán)特效
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)光點(diǎn)模糊漸變的自旋轉(zhuǎn)圓環(huán)特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android實(shí)現(xiàn)面包屑功能的代碼(支持Fragment聯(lián)動(dòng))
這篇文章主要介紹了Android實(shí)現(xiàn)面包屑功能的代碼(支持Fragment聯(lián)動(dòng)),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05Android 中Notification彈出通知實(shí)現(xiàn)代碼
NotificationManager 是狀態(tài)欄通知的管理類,負(fù)責(zé)發(fā)通知、清除通知等操作。接下來通過本文給大家介紹Android 中Notification彈出通知實(shí)現(xiàn)代碼,需要的的朋友參考下吧2017-08-08Android 8.0中一些坑以及對(duì)應(yīng)的解決方法
這篇文章主要給大家介紹了關(guān)于Android 8.0中一些坑以及對(duì)應(yīng)的解決方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09Android Activity切換(跳轉(zhuǎn))時(shí)出現(xiàn)黑屏的解決方法 分享
Android Activity切換(跳轉(zhuǎn))時(shí)出現(xiàn)黑屏的解決方法 分享,需要的朋友可以參考一下2013-06-06