最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)容如下

一、簡(jiǎn)介

二、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();

吐司

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

相關(guān)文章

最新評(píng)論

咸丰县| 双柏县| 黔西县| 平武县| 乌鲁木齐市| 淳安县| 清徐县| 青川县| 赤城县| 周宁县| 台江县| 大理市| 武汉市| 古蔺县| 喀喇| 泰兴市| 韶山市| 同德县| 孝义市| 邵阳县| 山阴县| 丹凤县| 方城县| 广德县| 金寨县| 南汇区| 长宁区| 繁峙县| 丹凤县| 井冈山市| 闽清县| 抚顺市| 陆河县| 阳春市| 吕梁市| 贵定县| 呼和浩特市| 银川市| 栾川县| 施秉县| 凉山|