android實(shí)現(xiàn)單選按鈕功能
在我們平時(shí)在注冊(cè)個(gè)人信息的時(shí)候,經(jīng)常會(huì)讓我們選擇是男生還是女生,那么這個(gè)單選框在Android中是怎么實(shí)現(xiàn)的呢?現(xiàn)在我們就來(lái)學(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 中,否則無(wú)法實(shí)現(xiàn)多選一的效果.
技巧:要面向RadioGroup 編程,不要面向RaidoButton 編程,否則將增加很大代碼量
android:orientation="vertical":執(zhí)行按鈕組的方向,默認(rèn)值是vertical.
RadioGroup的父類(lèi)時(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è)簡(jiǎn)單的單選框的實(shí)現(xiàn),希望對(duì)大家會(huì)有所幫助。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程實(shí)現(xiàn)擦除Bitmap中某一塊的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)擦除Bitmap中某一塊的方法,涉及Android操作Bitmap顏色像素值調(diào)整的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
android 仿微信demo——微信消息界面實(shí)現(xiàn)(服務(wù)端)
本系列文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧,希望能給你們提供幫助2021-06-06
Android四大組件之BroadcastReceiver詳解
今天小編就為大家分享一篇關(guān)于Android四大組件之BroadcastReceiver詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-01-01
Android智能指針輕量級(jí)Light Pointer初識(shí)
這篇文章主要為大家介紹了Android智能指針輕量級(jí)Light Pointer初識(shí)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android實(shí)現(xiàn)系統(tǒng)打印功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)系統(tǒng)打印功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12

