android 的常用控件

来源:互联网 发布:超级基因优化液压缩 编辑:程序博客网 时间:2024/05/18 02:15

本实例实现了单项选择,多项选择,单项选择按钮必须包括在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() {@Overridepublic void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {if (redCheck.isChecked()) {DisplayToast("你选择的是" + redCheck.getText());}}});blueCheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic 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) {// 显示ToastToast 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>

原创粉丝点击