android checkbox和radio学习

checkbox没有什么好说就这样
public class MainActivity extends Activity
{
	private CheckBox cbJava;
	private CheckBox cbCSharp;
	private CheckBox cbAbap;
	private CheckBox cbPhp;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		cbJava=(CheckBox)findViewById(R.id.cbJava);
		cbCSharp=(CheckBox)findViewById(R.id.cbCSharp);
		cbAbap=(CheckBox)findViewById(R.id.cbAbap);
		cbPhp=(CheckBox)findViewById(R.id.cbPhp);
		cbJava.setOnCheckedChangeListener(listener);
		cbCSharp.setOnCheckedChangeListener(listener);
		cbAbap.setOnCheckedChangeListener(listener);
		cbPhp.setOnCheckedChangeListener(listener);
	}
	private OnCheckedChangeListener listener = new OnCheckedChangeListener()
	{
		@Override
		public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
		{
			if (buttonView.getId()==R.id.cbJava)
			{
				if (isChecked)
				{
					Toast.makeText(MainActivity.this, "你学过Java", Toast.LENGTH_LONG).show();
				}
			}
			else if (buttonView.getId()==R.id.cbCSharp)
			{
				if (isChecked)
				{
					Toast.makeText(MainActivity.this, "你学过C#", Toast.LENGTH_LONG).show();
				}
			}
			else if (buttonView.getId()==R.id.cbAbap)
			{
				if (isChecked)
				{
					Toast.makeText(MainActivity.this, "你学过Abap", Toast.LENGTH_LONG).show();
				}
			}
			else if (buttonView.getId()==R.id.cbPhp)
			{
				if (isChecked)
				{
					Toast.makeText(MainActivity.this, "你学过Php", Toast.LENGTH_LONG).show();
				}
			}
			
		}
	};
}

 

raidoButton比较特殊要放到一个RadioGroup里面,然后实现RadioGroup中的setOnCheckedChangeListener方法,达到点击改变的radiobutton的值的效果:

 

public class MainActivity extends Activity
{
	private RadioGroup rgGender;
	private RadioGroup rgQuestion;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		rgGender=(RadioGroup)findViewById(R.id.rgGender);
		rgGender.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId)
			{
				if (checkedId == R.id.rbBoy)
				{
					Toast.makeText(MainActivity.this, "男孩", Toast.LENGTH_LONG).show();
				}
				else if (checkedId == R.id.rbGirl) 
				{
					Toast.makeText(MainActivity.this, "女孩", Toast.LENGTH_LONG).show();
				}
			}
		});
		rgQuestion=(RadioGroup)findViewById(R.id.rgQuestion);
		rgQuestion.setOnCheckedChangeListener(new OnCheckedChangeListener()
		{
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId)
			{
				if (checkedId == R.id.rbYes)
				{
					Toast.makeText(MainActivity.this, "毕业了", Toast.LENGTH_LONG).show();
				}
				else if (checkedId == R.id.rbNo) 
				{
					Toast.makeText(MainActivity.this, "没有毕业", Toast.LENGTH_LONG).show();
				}
				
			}
		});
	}
}
 

<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="你的性别"
	    android:textSize="20sp"
	    android:textStyle="bold"
	    android:textColor="#FFFFFF"
	    />
	<RadioGroup
		android:id="@+id/rgGender"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		>
		<RadioButton
			android:id="@+id/rbBoy"
			android:layout_height="wrap_content"
			android:layout_width="wrap_content"
			android:text="男孩"
			android:textSize="18sp"
			android:button="@null"                    
			android:drawableRight="@android:drawable/btn_radio"/>
			/>
		<RadioButton
			android:id="@+id/rbGirl"
			android:layout_height="wrap_content"
			android:layout_width="wrap_content"
			android:text="女孩"
			android:textSize="18sp"                 
			android:drawableTop="#006699"
			/>
	</RadioGroup>
	<View
	 	android:layout_width="match_parent"
	 	android:layout_height="1dp"
	 	android:background="#FFFFFF"
	 	/>
	 <TextView  
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="你毕业了吗"
	    android:textSize="20sp"
	    android:textStyle="bold"
	    android:textColor="#FFFFFF"
	    />
	<RadioGroup
		android:id="@+id/rgQuestion"
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		>
		<RadioButton
			android:id="@+id/rbYes"
			android:layout_height="wrap_content"
			android:layout_width="wrap_content"
			android:text="毕业了"
			android:textSize="18sp"
			/>
		<RadioButton
			android:id="@+id/rbNo"
			android:layout_height="wrap_content"
			android:layout_width="wrap_content"
			android:text="没有毕业"
			android:textSize="18sp"
			/>
	</RadioGroup>
</LinearLayout>
 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值