【移動應用開發(fā)技術】android怎么實現(xiàn)單選按鈕功能_第1頁
【移動應用開發(fā)技術】android怎么實現(xiàn)單選按鈕功能_第2頁
【移動應用開發(fā)技術】android怎么實現(xiàn)單選按鈕功能_第3頁
免費預覽已結束,剩余1頁可下載查看

付費下載

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

【移動應用開發(fā)技術】android怎么實現(xiàn)單選按鈕功能

這篇文章主要介紹android怎么實現(xiàn)單選按鈕功能,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!首先我們要明白實現(xiàn)這樣一個效果需要哪幾部?1、在layout布局文件中建立一個文件,我起的名字為activity_radio.xml代碼為:<?xml

version="1.0"

encoding="utf-8"?>

<LinearLayout

xmlns:android="/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

moncontroldemo02;

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ā)生變化時自動調用的方法

*

@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("===女性

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論