源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

Android控件RadioButton实现多选一功能

  • 时间:2020-01-02 09:20 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android控件RadioButton实现多选一功能
RadioButton实现多选一功能的方法,具体内容如下 [b]一、简介[/b] [b]二、RadioButton实现多选一方法[/b] 1、将多个RadioButton放在一个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里面取出每个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、检查每个RadioButton是否被选取
 if (radio.isChecked()) {

      break;
     } 
4、取出被选取的那个RadioButton里面的值
Toast.makeText(Activity01.this, radio.getText(),
        Toast.LENGTH_LONG).show();
[b] 三、代码实例[/b] 效果图:  代码: 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实现多选一方法
   * 1、将多个RadioButton放在一个RadioGroup里面
   * 2、在RadioGroup里面取出每个RadioButton 
   * 3、检查每个RadioButton是否被选取
   * 4、取出被选取的那个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" 设置颜色,直接用#FFFFFF 2、 android:layout_gravity="center_horizontal" 文字居中显示 3、 RadioButton在RadioGroup里面实现多选一 4、 android:background="@android:color/black" 设置黑色,系统自带颜色 5、 int len = radioGroup1.getChildCount(); RadioGroup获取孩子数量 6、 RadioButton radio = (RadioButton) radioGroup1.getChildAt(i); RadioGroup获取孩子 7、 if (radio.isChecked()) 判断RadioButton是否被选取 8、 Toast.makeText(Activity01.this, radio.getText(),Toast.LENGTH_LONG).show(); 吐司 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部