Android开发之选择按钮RadioButton

RadioButton是Button中的一种,主要在开发中用于布局,可以让用户进行单项或者多项选择

package com.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class RadioActivity extends Activity {
    /** Called when the activity is first created. */
	
	private RadioGroup radioGroup;
	private RadioButton maleButton;
	private RadioButton femaleButton;
	
	private CheckBox swimBox;
	private CheckBox runBox;
	private CheckBox ballBox;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //根据id获取控件对象
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        maleButton = (RadioButton) findViewById(R.id.maleButton);
        femaleButton = (RadioButton) findViewById(R.id.femaleButton);
        
        swimBox = (CheckBox) findViewById(R.id.swim);
        runBox = (CheckBox) findViewById(R.id.run);
        ballBox = (CheckBox) findViewById(R.id.ball);
        
        //匿名内部类
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
        	//当用户点击单选按钮时,会立刻触发该事件
        	//会传入两个参数,一个是所在组,一个是组里面所点击的单选按钮id
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				//判断用户所点击的是哪一个
				if(maleButton.getId() == checkedId){
					Toast.makeText(RadioActivity.this, "male", Toast.LENGTH_SHORT).show();
				}else if(femaleButton.getId() == checkedId){
					Toast.makeText(RadioActivity.this, "female", Toast.LENGTH_SHORT).show();
				}
			}
		});
        //为每一个复选按钮增加监听
        swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					Toast.makeText(RadioActivity.this, "swim is checked", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(RadioActivity.this, "swim is unchecked", Toast.LENGTH_SHORT).show();
				}
			}
		});
        runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					Toast.makeText(RadioActivity.this, "run is checked", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(RadioActivity.this, "run is unchecked", Toast.LENGTH_SHORT).show();
				}
			}
		});
        ballBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					Toast.makeText(RadioActivity.this, "ball is checked", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(RadioActivity.this, "ball is unchecked", Toast.LENGTH_SHORT).show();
				}
			}
		});
    }
}




<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<TextView  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="@string/hello"
	    />
	<RadioGroup
		android:id="@+id/radioGroup"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		>
		<RadioButton
			android:id="@+id/maleButton"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="@string/male"
			/>
		<RadioButton
			android:id="@+id/femaleButton"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="@string/female"
			/>
	</RadioGroup>
	<CheckBox
		android:id="@+id/swim"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="@string/swim"
		/>
	<CheckBox
		android:id="@+id/run"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="@string/run"
		/>
	<CheckBox
		android:id="@+id/ball"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="@string/ball"
		/>
</LinearLayout>








更多关于RadioButton

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值