android實現(xiàn)單選按鈕功能
在我們平時在注冊個人信息的時候,經(jīng)常會讓我們選擇是男生還是女生,那么這個單選框在Android中是怎么實現(xiàn)的呢?現(xiàn)在我們就來學習一下吧
首先我們要明白實現(xiàn)這樣一個效果需要哪幾部?

1、在layout布局文件中建立一個文件,我起的名字為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 要想實現(xiàn)多選一的效果必須放到RadioGroup 中,否則無法實現(xiàn)多選一的效果.
技巧:要面向RadioGroup 編程,不要面向RaidoButton 編程,否則將增加很大代碼量
android:orientation="vertical":執(zhí)行按鈕組的方向,默認值是vertical.
RadioGroup的父類時LinearLayout,但方向的默認值不再是線性布局的水平方向了,而是改成了垂直方向.
-->
<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中實現(xiàn)細節(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);
}
/**
* 當單選按鈕的狀態(tài)發(fā)生變化時自動調(diào)用的方法
* @param group 單選按鈕所在的按鈕組的對象
* @param checkedId 用戶選中的單選按鈕的id值
*/
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//得到用戶選中的 RadioButton 對象
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:
//當用戶點擊男性按鈕時執(zhí)行的代碼
System.out.println("===男性===");
break;
case R.id.radioButton_female:
//當用戶點擊女性按鈕時執(zhí)行的代碼
System.out.println("===女性===");
break;
}
System.out.println("===onCheckedChanged(RadioGroup group="+group+", int checkedId="+checkedId+")==");
}
}那么以上就是一個簡單的單選框的實現(xiàn),希望對大家會有所幫助。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android 使用Vitamio打造自己的萬能播放器(6)——在線播放(播放列表)
本文主要介紹Android Vitamino在線播放列表,這里給大家提供效果圖和實例代碼以便大家參考學習,希望能幫助開發(fā)Android視頻播放的朋友2016-07-07
Android開發(fā)之判斷有無虛擬按鍵(導航欄)的實例
下面小編就為大家分享一篇Android開發(fā)之判斷有無虛擬按鍵(導航欄)的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android實現(xiàn)光點模糊漸變的自旋轉(zhuǎn)圓環(huán)特效
這篇文章主要為大家詳細介紹了Android實現(xiàn)光點模糊漸變的自旋轉(zhuǎn)圓環(huán)特效,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-06-06
Android實現(xiàn)面包屑功能的代碼(支持Fragment聯(lián)動)
這篇文章主要介紹了Android實現(xiàn)面包屑功能的代碼(支持Fragment聯(lián)動),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
Android 中Notification彈出通知實現(xiàn)代碼
NotificationManager 是狀態(tài)欄通知的管理類,負責發(fā)通知、清除通知等操作。接下來通過本文給大家介紹Android 中Notification彈出通知實現(xiàn)代碼,需要的的朋友參考下吧2017-08-08
Android Activity切換(跳轉(zhuǎn))時出現(xiàn)黑屏的解決方法 分享
Android Activity切換(跳轉(zhuǎn))時出現(xiàn)黑屏的解決方法 分享,需要的朋友可以參考一下2013-06-06

