android實現(xiàn)單選按鈕功能
在我們平時在注冊個人信息的時候,經(jīng)常會讓我們選擇是男生還是女生,那么這個單選框在Android中是怎么實現(xiàn)的呢?現(xiàn)在我們就來學(xué)習(xí)一下吧
首先我們要明白實現(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程實現(xiàn)擦除Bitmap中某一塊的方法
這篇文章主要介紹了Android編程實現(xiàn)擦除Bitmap中某一塊的方法,涉及Android操作Bitmap顏色像素值調(diào)整的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11
android 仿微信demo——微信消息界面實現(xiàn)(服務(wù)端)
本系列文章主要介紹了微信小程序-閱讀小程序?qū)嵗╠emo),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望能給你們提供幫助2021-06-06
Android四大組件之BroadcastReceiver詳解
今天小編就為大家分享一篇關(guān)于Android四大組件之BroadcastReceiver詳解,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01

