android 的常用控件

本实例实现了单项选择,多项选择,单项选择按钮必须包括在RadionGroup


--------------AndroidwidgetActivity------------

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

public class AndroidwidgetActivity extends Activity {
	/** Called when the activity is first created. */
	private RadioGroup radioSex;
	private RadioButton femaleButton;
	private RadioButton maleButton;
	private CheckBox redCheck;
	private CheckBox blueCheck;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
        //得到main.xml中的控件
		radioSex = (RadioGroup) findViewById(R.id.sex);
		femaleButton = (RadioButton) findViewById(R.id.female);
		maleButton = (RadioButton) findViewById(R.id.male);
		redCheck = (CheckBox) findViewById(R.id.red);
		blueCheck = (CheckBox) findViewById(R.id.blue);
       //给RadioGroup添加监听器
		radioSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

			public void onCheckedChanged(RadioGroup group, int checkedId) {

				if (checkedId == femaleButton.getId()) {
					DisplayToast("你选择的是" + femaleButton.getText());
				} else {
					DisplayToast("你选择的是" + maleButton.getText());
				}

			}
		});
        //给CheckBox添加控件
		redCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (redCheck.isChecked()) {
					DisplayToast("你选择的是" + redCheck.getText());
				}
			}
		});

		blueCheck
				.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

					@Override
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						if (blueCheck.isChecked()) {
							DisplayToast("你选择的是" + blueCheck.getText());
						}
					}
				});

	}

	/**
	 * Toast 用法
	 * 
	 * public static Toast makeText (Context context, int resId, int duration)
	 * 
	 * context 显示的内容对象 resId 显示的字符串的id duration 显示的时间长度. LENGTH_SHORT 或
	 * LENGTH_LONG
	 * 
	 */
	public void DisplayToast(String str) {
		// 显示Toast
		Toast toast = Toast.makeText(this, str, Toast.LENGTH_LONG);
		// 设置Toast显示的位置
		toast.setGravity(Gravity.TOP, 0, 220);
		toast.show();
	}
}
-------------main.xml----------


<?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"
    >
    <RadioGroup
      android:id="@+id/sex"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical">
      <RadioButton 
          android:id="@+id/female"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Female" />
      <RadioButton 
          android:id="@+id/male"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Male" />
    </RadioGroup>
    
    <CheckBox 
        android:id="@+id/red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="红色" />
        
    <CheckBox 
        android:id="@+id/blue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="蓝色" />
</LinearLayout>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值