Switch-RadioButton-CheckBox例子

<span style="font-size: 18px; ">这里具体这么用我就不解释了,下面我就对他们做一个例子讲解</span>

public class MainActivity extends Activity {
private RadioButton radio1,radio2,radio3;
private CheckBox check;
private Switch switch1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		radio1=(RadioButton) findViewById(R.id.radio1);
		radio2=(RadioButton) findViewById(R.id.radio2);
		radio3=(RadioButton) findViewById(R.id.radio3);
		check=(CheckBox) findViewById(R.id.check);
		switch1=(Switch) findViewById(R.id.switch1);
	}
	public void radioClick(View v){
		RadioButton button=(RadioButton) v;
		boolean isChecked=button.isChecked();
		switch (v.getId()){
		case R.id.radio1:
			if(isChecked){
			Toast.makeText(MainActivity.this, button.getText(), Toast.LENGTH_LONG).show();	
			}
			break;
case R.id.radio2:
	if(isChecked){
		Toast.makeText(MainActivity.this, button.getText(), Toast.LENGTH_LONG).show();	
		}
			break;
case R.id.radio3:
	if(isChecked){
		Toast.makeText(MainActivity.this, button.getText(), Toast.LENGTH_LONG).show();	
		}
	break;
		default:
			break;
		}
	}
	public void switchClick(View v){
		boolean isChecked=((Switch)v).isChecked();
		switch (v.getId()) {
		case R.id.switch1:
			if(isChecked){
				Toast.makeText(MainActivity.this, "打开", Toast.LENGTH_LONG).show();	
				}else{
					Toast.makeText(MainActivity.this, "关闭", Toast.LENGTH_LONG).show();	
				}
			break;

		default:
			break;
		}
	}
	public void checkClick(View v){
		CheckBox box=(CheckBox) v;
		boolean isChecked=box.isChecked();
		switch (v.getId()) {
		case R.id.check:
			if(isChecked){
				Toast.makeText(MainActivity.this, check.getText(), Toast.LENGTH_LONG).show();	
				}
			break;

		default:
			break;
		}
	}
}
最后说下布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
              android:onClick="radioClick"
            android:checked="true"
            android:text="男" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
              android:onClick="radioClick"
            android:text="女" />

        <RadioButton
            android:id="@+id/radio3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
             android:onClick="radioClick"
            android:text="保密" />
    </RadioGroup>
    
<Switch
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textOn="打开"
    android:textOff="关闭"
    android:onClick="switchClick"
    android:text="开启功能"/>
<CheckBox 
    android:id="@+id/check"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
     android:onClick="checkClick"
    android:text="是否有用"/>

</LinearLayout>

这里是代码的连接:http://download.csdn.net/detail/kluing/7623545


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值