Android学习笔记——EditText、RadioGroup、CheckBox和Spinner的使用。

这篇博客详细介绍了Android开发中常见的UI组件使用,包括EditText的文本输入功能,RadioGroup的单选按钮操作,CheckBox的复选框选择,以及Spinner的下拉菜单选择。通过实例展示了如何在主界面配置这些组件,特别是Spinner如何实现血型选择并动态显示所选血型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、EditText:

主界面:

<?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"
    >
	<TextView  
	android:id="@+id/TextView01"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />
  <EditText
  android:id="@+id/EditText01"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:textSize="18sp"	
  android:layout_x="29px"
  android:layout_y="33px"
  />
</LinearLayout>
业务逻辑代码如下:

package com.edittext;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class EditTextActivity extends Activity{
	private TextView	m_TextView;
	private EditText	m_EditText;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		m_TextView = (TextView) findViewById(R.id.TextView01);
		m_EditText = (EditText) findViewById(R.id.EditText01);

		m_TextView.setTextSize(20);
		/**
		 * 设置当m_EditText中为空时提示的内容 
		 * 在XML中同样可以实现:android:hint="请输入账号"
		 */
		m_EditText.setHint("请输入账号");
		
		/* 设置EditText事件监听 */
		m_EditText.setOnKeyListener(new EditText.OnKeyListener() {
			@Override
			public boolean onKey(View arg0, int arg1, KeyEvent arg2)
			{
				// TODO Auto-generated method stub
				// 得到文字,将其显示到TextView中
				m_TextView.setText("文本框中内容是:" + m_EditText.getText().toString());
				return false;
			}
		});
	}
}
2、RadioGroup

主界面配置如下:

<?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"
    >
	<TextView  
	android:id="@+id/TextView01"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
  <RadioGroup
    android:id="@+id/RadioGroup01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_x="3px"
    android:layout_y="54px" 
    >
    <RadioButton
      android:id="@+id/RadioButton1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/RadioButton1"
    />
    <RadioButton
      android:id="@+id/RadioButton2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/RadioButton2"
    />
    <RadioButton
      android:id="@+id/RadioButton3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/RadioButton3"
    />
    <RadioButton
      android:id="@+id/RadioButton4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/RadioButton4"
    />
    </RadioGroup>    
</LinearLayout>
配置了一组RadioButton,组成一个Group。

package com.rediogroup;

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

public class RedioGroupActivity extends Activity{
	/**
	 * 创建TextView对象
	 * 创建RadioGroup对象
	 * 创建四个RadioButton对象
	 */
	TextView m_TextView;
	RadioGroup m_RadioGroup;
	RadioButton m_Radio1,m_Radio2,m_Radio3,m_Radio4;
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		/**
		 * 获取TextView对象
		 * 获取RadioGroup对象
		 * 获取四个RadioButton对象
		 */
		m_TextView = (TextView) findViewById(R.id.TextView01);
		m_RadioGroup = (RadioGroup) findViewById(R.id.RadioGroup01);
		m_Radio1 = (RadioButton) findViewById(R.id.RadioButton1);
		m_Radio2 = (RadioButton) findViewById(R.id.RadioButton2);
		m_Radio3 = (RadioButton) findViewById(R.id.RadioButton3);
		m_Radio4 = (RadioButton) findViewById(R.id.RadioButton4);
		
		m_RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedID) {
				if(checkedID == m_Radio2.getId()){
					DisplayToast("正确答案:"+m_Radio2.getText()+",恭喜你回答正确!");
				}else{
					DisplayToast("回答错误!");
				}
			}
		});
	}
	
	public void DisplayToast(String text){
		Toast toast = Toast.makeText(this, text, Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 20, 220);
		toast.show();
	}
}
3、CheckBox

主界面配置如下:

<?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"
    >
<TextView  
  	android:id="@+id/TextView1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<CheckBox
  android:id="@+id/CheckBox1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/CheckBox1"
>
</CheckBox>
<CheckBox
  android:id="@+id/CheckBox2"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/CheckBox2"
>
</CheckBox>
<CheckBox
android:id="@+id/CheckBox3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/CheckBox3"
>
</CheckBox>
<CheckBox
android:id="@+id/CheckBox4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/CheckBox4"
>
</CheckBox>
    <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="提交"
    >
    </Button>
</LinearLayout>
业务逻辑中当选择一个item时会弹出相应的值。提交时统计选择的个数。

package com.checkbox;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

public class CheckBoxActivity extends Activity{
	//用来显示题目
		TextView	m_TextView1;
		//“提交按钮”
		Button		m_Button1;
		//4个多选项
		CheckBox	m_CheckBox1;
		CheckBox	m_CheckBox2;
		CheckBox	m_CheckBox3;
		CheckBox	m_CheckBox4;
		
		/** Called when the activity is first created. */
		@Override
		public void onCreate(Bundle savedInstanceState)
		{
			super.onCreate(savedInstanceState);
			setContentView(R.layout.main);

			m_TextView1 = (TextView) findViewById(R.id.TextView1);
			m_Button1 = (Button) findViewById(R.id.button1);

			/* 取得每个CheckBox对象 */
			m_CheckBox1 = (CheckBox) findViewById(R.id.CheckBox1);
			m_CheckBox2 = (CheckBox) findViewById(R.id.CheckBox2);
			m_CheckBox3 = (CheckBox) findViewById(R.id.CheckBox3);
			m_CheckBox4 = (CheckBox) findViewById(R.id.CheckBox4);

			//对每个选项设置事件监听
			m_CheckBox1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
				{
					if(m_CheckBox1.isChecked())
					{
						DisplayToast("你选择了:"+m_CheckBox1.getText());
					}
				}
			});
			////////////////////
			m_CheckBox2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
				{
					if(m_CheckBox2.isChecked())
					{
						DisplayToast("你选择了:"+m_CheckBox2.getText());
					}
				}
			});
			/////////////////
			m_CheckBox3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
				{
					if(m_CheckBox3.isChecked())
					{
						DisplayToast("你选择了:"+m_CheckBox3.getText());
					}
				}
			});
			////////////////
			m_CheckBox4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
				{
					if(m_CheckBox4.isChecked())
					{
						DisplayToast("你选择了:"+m_CheckBox4.getText());
					}
				}
			});
			//对按钮设置事件监听
			m_Button1.setOnClickListener(new Button.OnClickListener() {
				public void onClick(View v)
				{
					int num = 0;
					if(m_CheckBox1.isChecked())
					{
						num++;
					}
					if(m_CheckBox2.isChecked())
					{
						num++;
					}
					if(m_CheckBox3.isChecked())
					{
						num++;
					}
					if(m_CheckBox4.isChecked())
					{
						num++;
					}
					DisplayToast("谢谢参与!你一共选择了"+num+"项!");
				}
			});
		}
		
		/* 显示Toast  */
		public void DisplayToast(String str)
		{
			Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
			//设置toast显示的位置
			toast.setGravity(Gravity.TOP, 0, 220);
			//显示该Toast
			toast.show();
		}
}
4、Spinner

下拉菜单,配置了一组血型,当选择一个血型时再上方显示。

<?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"
    >
<TextView  
	android:id="@+id/TextView1"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<Spinner
  android:id="@+id/Spinner1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
/>
</LinearLayout>
业务逻辑:

package com.spinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class SpinnerActivity extends Activity {
	private static final String[]	m_Countries	= { "O型", "A型", "B型", "AB型", "其他" };

	private TextView				m_TextView;
	private Spinner					m_Spinner;
	private ArrayAdapter<String>	adapter;


	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		m_TextView = (TextView) findViewById(R.id.TextView1);
		m_Spinner = (Spinner) findViewById(R.id.Spinner1);

		//将可选内容与ArrayAdapter连接
		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, m_Countries);

		//设置下拉列表的风格
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		
		//将adapter添加到m_Spinner中
		m_Spinner.setAdapter(adapter);

		//添加Spinner事件监听
		m_Spinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
			{
				m_TextView.setText("你的血型是:" + m_Countries[arg2]);
				//设置显示当前选择的项
				arg0.setVisibility(View.VISIBLE);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0)
			{
				// TODO Auto-generated method stub
			}

		});
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值