ToggleButton的使用,RadioGroup的使用和监听,

一、我们先使用ToggleButton(开关按钮)实现一个布局的横向和纵向排列。

布局文件如下:

 <ToggleButton
        android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/view"
        android:checked="true"
        android:textOff="横向排列"
        android:textOn="纵向排列" />

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/toggle"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button1" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="button2" />
    </LinearLayout>

activity中的代码如下:

toggleButton = (ToggleButton) this.findViewById(R.id.toggle);
		final LinearLayout layout = (LinearLayout) this
				.findViewById(R.id.layout);
		toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			//通过检查是否被打开和关闭,用isChecked来判断。
			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				// TODO Auto-generated method stub
				if (isChecked) {
					layout.setOrientation(LinearLayout.VERTICAL);
				} else {
					layout.setOrientation(LinearLayout.HORIZONTAL);
				}
			}
		});

二、用 radioGroup来选择性别。

布局文件:

<RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/view2" >

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

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/female"
            android:text="女" />
    </RadioGroup>

activity中实现代码,方法一:

final RadioButton manRadioButton = (RadioButton) this
				.findViewById(R.id.man);
		final RadioButton femaleRadioButton = (RadioButton) this
				.findViewById(R.id.female);
		//radioGroup是把里面所有的button都设置id,通过判断id,来获得所选性别。
		radioGroup = (RadioGroup) this.findViewById(R.id.radiogroup);
		//注意的是,在建立监听器时,必须new RadioGroup.OnCheckChangeListener(),自动生成的代码不会有 RadioGroup.;
		radioGroup
				.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(RadioGroup group, int checkedId) {
						// TODO Auto-generated method stub
						//判断选中的id。
						if (checkedId == manRadioButton.getId()) {
							Toast.makeText(getApplicationContext(), "男", 1)
									.show();
						} else if (checkedId == femaleRadioButton.getId()) {
							Toast.makeText(getApplicationContext(), "女", 1)
									.show();
						}
					}
				});
方法二:

/**
		 * 这个方法,不用给每个子radioButton设置id,对一个普通的button按钮设置监听事件,
		 * 通过radioGroup得getChildCount()方法获得子radioButton的个数, 利用一个for循环,获得每一个
		 */
		submitButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int len = radioGroup.getChildCount();
				for (int i = 0; i < len; i++) {
					RadioButton radioButton = (RadioButton) radioGroup
							.getChildAt(i);
					if (radioButton.isChecked()) {
						String msg = "方法二:";
						msg += radioButton.getText().toString().trim();
						Toast.makeText(getApplicationContext(), msg, 1).show();
					}

				}
			}
		});










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值