欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android控件RadioButton實(shí)現(xiàn)多選一功能

 更新時(shí)間:2021年05月12日 10:45:37   作者:飯飯_fan  
這篇文章主要為大家詳細(xì)介紹了Android控件RadioButton實(shí)現(xiàn)多選一功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

RadioButton實(shí)現(xiàn)多選一功能的方法,具體內(nèi)容如下

一、簡介

二、RadioButton實(shí)現(xiàn)多選一方法

1、將多個(gè)RadioButton放在一個(gè)RadioGroup里面

<RadioGroup
  android:id="@+id/radioGroup1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >

  <RadioButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="男"
   android:textColor="#FFFFFF" />

  <RadioButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="女"
   android:textColor="#FFFFFF" />
 </RadioGroup>

2、在RadioGroup里面取出每個(gè)RadioButton

public void onClick(View v) {
    // TODO Auto-generated method stub
    int len = radioGroup1.getChildCount();
    for (int i = 0; i < len; i++) {
     RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);11     }
   }

3、檢查每個(gè)RadioButton是否被選取

 if (radio.isChecked()) {

      break;
     } 

4、取出被選取的那個(gè)RadioButton里面的值

Toast.makeText(Activity01.this, radio.getText(),
        Toast.LENGTH_LONG).show();

三、代碼實(shí)例

效果圖:

代碼:

fry.Activity01

package fry;

import com.example.RadioButtonDemo1.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class Activity01 extends Activity {
 private Button btn_chooseGender;
 private RadioGroup radioGroup1;
 private TextView tv_answer;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  // TODO Auto-generated method stub
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity01);

  btn_chooseGender = (Button) findViewById(R.id.btn_chooseGender);
  radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
  tv_answer = (TextView) findViewById(R.id.tv_answer);
  /*
   * RadioButton實(shí)現(xiàn)多選一方法
   * 1、將多個(gè)RadioButton放在一個(gè)RadioGroup里面
   * 2、在RadioGroup里面取出每個(gè)RadioButton 
   * 3、檢查每個(gè)RadioButton是否被選取
   * 4、取出被選取的那個(gè)RadioButton里面的值
   */
  btn_chooseGender.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    int len = radioGroup1.getChildCount();
    for (int i = 0; i < len; i++) {
     RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);
     if (radio.isChecked()) {
      Toast.makeText(Activity01.this, radio.getText(),
        Toast.LENGTH_LONG).show();
      break;
     }
    }
   }
  });
 }
}

/RadioButtonDemo1/res/layout/activity01.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:background="@android:color/black"
 android:orientation="vertical" >

 <TextView
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="性別"
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:layout_gravity="center_horizontal"
  android:textColor="#FFFFFF" />

 <RadioGroup
  android:id="@+id/radioGroup1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >

  <RadioButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="男"
   android:textColor="#FFFFFF" />

  <RadioButton
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="女"
   android:textColor="#FFFFFF" />
 </RadioGroup>

 <Button 
  android:id="@+id/btn_chooseGender"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="選擇性別"
  android:textColor="#FFFFFF" />
  />
  
 <TextView
  android:id="@+id/tv_answer"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text=""
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:layout_gravity="center_horizontal"
  android:textColor="#FFFFFF" />
</LinearLayout>

四、收獲

1、

android:textColor="#FFFFFF"

設(shè)置顏色,直接用#FFFFFF

2、

android:layout_gravity="center_horizontal"

文字居中顯示

3、

RadioButton在RadioGroup里面實(shí)現(xiàn)多選一

4、

android:background="@android:color/black"

設(shè)置黑色,系統(tǒng)自帶顏色

5、

int len = radioGroup1.getChildCount();

RadioGroup獲取孩子數(shù)量

6、

RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);

RadioGroup獲取孩子

7、

if (radio.isChecked())

判斷RadioButton是否被選取

8、

Toast.makeText(Activity01.this, radio.getText(),Toast.LENGTH_LONG).show();

吐司

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

  • Android 監(jiān)聽屏幕是否鎖屏的實(shí)例代碼

    Android 監(jiān)聽屏幕是否鎖屏的實(shí)例代碼

    今天小編通過本文給大家分享android如何監(jiān)聽手機(jī)屏幕是否鎖屏。實(shí)現(xiàn)方法很簡單,需要的朋友參考下吧
    2017-09-09
  • Android 判斷是否是是全漢字、全字母、全數(shù)字、數(shù)字和字母等(代碼)

    Android 判斷是否是是全漢字、全字母、全數(shù)字、數(shù)字和字母等(代碼)

    這篇文章主要介紹了Android 判斷是否是是全漢字、全字母、全數(shù)字、數(shù)字和字母等的實(shí)例代碼,需要的朋友可以參考下
    2016-12-12
  • Android 媒體庫數(shù)據(jù)更新方法總結(jié)

    Android 媒體庫數(shù)據(jù)更新方法總結(jié)

    這篇文章主要介紹了Android 媒體庫數(shù)據(jù)更新方法總結(jié)的相關(guān)資料,需要的朋友可以參考下
    2017-04-04
  • Android實(shí)現(xiàn)關(guān)機(jī)重啟的方法分享

    Android實(shí)現(xiàn)關(guān)機(jī)重啟的方法分享

    這篇文章主要介紹了Android實(shí)現(xiàn)關(guān)機(jī)重啟的方法,需要的朋友可以參考下
    2014-02-02
  • Android開發(fā) Activity和Fragment詳解

    Android開發(fā) Activity和Fragment詳解

    本文主要介紹Android開發(fā) Activity和Fragment,這里對(duì)Activity和Fragment的知識(shí)做了詳細(xì)講解,并附簡單代碼示例,有興趣的小伙伴可以參考下
    2016-08-08
  • Flow如何解決背壓問題的方法詳解

    Flow如何解決背壓問題的方法詳解

    這篇文章主要為大家介紹了Flow如何解決背壓問題的方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Android模擬實(shí)現(xiàn)華為系統(tǒng)升級(jí)進(jìn)度條

    Android模擬實(shí)現(xiàn)華為系統(tǒng)升級(jí)進(jìn)度條

    這篇文章主要介紹了如何通過Android模擬實(shí)現(xiàn)華為在系統(tǒng)升級(jí)時(shí)顯示的進(jìn)度條。文中的實(shí)現(xiàn)過程講解詳細(xì),感興趣的小伙伴可以動(dòng)手試一試
    2022-01-01
  • android實(shí)現(xiàn)主動(dòng)連接和被動(dòng)連接的藍(lán)牙聊天功能

    android實(shí)現(xiàn)主動(dòng)連接和被動(dòng)連接的藍(lán)牙聊天功能

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)主動(dòng)連接和被動(dòng)連接的藍(lán)牙聊天功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • Android使用美團(tuán)多渠道打包方案詳解

    Android使用美團(tuán)多渠道打包方案詳解

    這篇文章主要介紹了Android使用美團(tuán)多渠道打包方案詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • 最新評(píng)論