android中的单选和多选按钮的使用

1.布局文件:

	<RadioGroup android:layout_width="wrap_content"
	    		android:layout_height="wrap_content"
	    		android:orientation="vertical"
	    		android:id="@+id/radioGroup">
	    <RadioButton android:layout_width="wrap_content"
	        		android:layout_height="wrap_content"
	        		android:id="@+id/radio1"
	        		android:text="@string/female"/>
	    <RadioButton android:layout_width="wrap_content"
	        		android:layout_height="wrap_content"
	        		android:id="@+id/radio2"
	        		android:text="@string/male"/>
	</RadioGroup>
	<CheckBox android:id="@+id/singBox"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:text="@string/sing"/>
	<CheckBox android:id="@+id/runBox"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:text="@string/run"/>
	<CheckBox android:id="@+id/danceBox"
	    	android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:text="@string/dance"/>

2.activity

package com.example.android1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class LinearLayOut extends Activity
{
	private RadioGroup radioGroup;
	private RadioButton radio1,radio2;
	private CheckBox runBox,singBox,danceBox;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button button=(Button)findViewById(R.id.button);
		button.setOnClickListener(new MyButtonListener());//绑定监听器
		radio1=(RadioButton)findViewById(R.id.radio1);
		radio2=(RadioButton)findViewById(R.id.radio2);
		radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
		runBox=(CheckBox)findViewById(R.id.runBox);
		singBox=(CheckBox)findViewById(R.id.singBox);
		danceBox=(CheckBox)findViewById(R.id.danceBox);
		//单选按钮监听器
		radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
		{
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId)
			{
				// TODO Auto-generated method stub
				if(radio1.getId()==checkedId)
				{
					System.out.println("女");
//					Toast.makeText(LinearLayout.class, "选择了女", Toast.LENGTH_SHORT).show();
					Toast.makeText(LinearLayOut.this, "选择了女", Toast.LENGTH_SHORT).show();
				}
				else if(radio2.getId()==checkedId)
				{
					System.out.println("男");
				}
			}
		});
		//复选框监听器,每一个checkbox都需要一个
		runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
		{
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				if(isChecked)
				{
					System.out.println("runBox is selected");
				}
				else
				{
					System.out.println("runBox is unselected");
				}
				
			}
		});
		singBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
		{
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				if(isChecked)
				{
					System.out.println("singBox is selected");
				}
				else
				{
					System.out.println("singBox is unselected");
				}
				
			}
		});
		danceBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
		{
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
			{
				if(isChecked)
				{
					System.out.println("danceBox is selected");
				}
				else
				{
					System.out.println("danceBox is unselected");
				}
				
			}
		});
	}
	class MyButtonListener implements OnClickListener
	{

		@Override
		public void onClick(View v)
		{
			Intent intent=new Intent();
			intent.setClass(LinearLayOut.this, TableLayout.class);
			LinearLayOut.this.startActivity(intent);
			
		}
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值