一、建立一个Android项目
二、具体步骤
1、先编辑string.xml文件
<resources>
<string name="hello">Hello World, LoginActivity!</string>
<string name="app_name">用户注册</string>
<string name="nameString">用户名:</string>
<string name="namevalue_hint">请输入英文字母、数字</string>
<string name="passString">密码:</string>
<string name="passvalue_hint">请输入密码</string>
<string name="ageString">年龄:</string>
<string name="agevalue_hint">请输入年龄</string>
<string name="sexString">性别:</string>
<string name="Man">男</string>
<string name="Woman">女</string>
<string name="favoriteString">喜好:</string>
<string name="cityString">城市:</string>
<string name="Beijing">北京</string>
<string name="ShangHai">上海</string>
<string name="GuangZhou">广州</string>
<string name="ShenZhen">深圳</string>
<string name="pingpang">乒乓球</string>
<string name="basketball">蓝球</string>
<string name="football">足球</string>
<string name="tennis">网球</string>
<string name="registerbutton">注册</string>
</resources>
2、再在res/values/目录下新建一个arrays.xml文件
<!-- lang: xml -->
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="cities">
<item></item>
<item>Beijing</item>
<item>Shanghai</item>
<item>Guangzhou</item>
<item>Shenzhen</item>
</string-array>
</resources>
3、编辑main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 用户名 -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/name"
android:layout_width="@dimen/TextViewWidth"
android:layout_height="wrap_content"
android:text="@string/nameString"
android:textSize="@dimen/fontsize" />
<EditText
android:id="@+id/namevalue"
android:layout_width="@dimen/EditTextWidth"
android:layout_height="wrap_content"
android:hint="@string/namevalue_hint"
android:layout_alignTop="@id/name"
android:layout_toRightOf="@id/name"
android:inputType="text" />
</RelativeLayout>
<!-- 密码 -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/password"
android:layout_width="@dimen/TextViewWidth"
android:layout_height="wrap_content"
android:text="@string/passString"
android:textSize="@dimen/fontsize" />
<EditText
android:id="@+id/passvalue"
android:layout_width="@dimen/EditTextWidth"
android:layout_height="wrap_content"
android:hint="@string/passvalue_hint"
android:layout_alignTop="@id/password"
android:layout_toRightOf="@id/password"
android:inputType="textPassword" />
</RelativeLayout>
<!-- 年龄 -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/ageLabel"
android:layout_width="@dimen/TextViewWidth"
android:layout_height="wrap_content"
android:text="@string/ageString"
android:textSize="@dimen/fontsize" />
<EditText
android:id="@+id/agevalue"
android:layout_width="@dimen/EditTextWidth"
android:layout_height="wrap_content"
android:hint="@string/agevalue_hint"
android:layout_alignTop="@id/ageLabel"
android:layout_toRightOf="@id/ageLabel"
android:inputType="number" />
</RelativeLayout>
<!-- 性别 -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/sexLabel"
android:layout_width="@dimen/TextViewWidth"
android:layout_height="wrap_content"
android:text="@string/sexString"
android:textSize="@dimen/fontsize" />
<RadioGroup
android:id="@+id/sexMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/sexLabel"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radioMan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Man" />
<RadioButton
android:id="@+id/radioWoman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:text="@string/Woman" />
</RadioGroup>
</RelativeLayout>
<!-- 爱好 -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/favoriteLabel"
android:layout_width="@dimen/TextViewWidth"
android:layout_height="wrap_content"
android:text="@string/favoriteString"
android:textSize="@dimen/fontsize" />
<CheckBox
android:id="@+id/checkboxpingpang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/favoriteLabel"
android:text="@string/pingpang" />
<CheckBox
android:id="@+id/checkboxfootball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/checkboxpingpang"
android:text="@string/football" />
<CheckBox
android:id="@+id/checkboxbasketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/checkboxpingpang"
android:layout_below="@id/checkboxpingpang"
android:text="@string/basketball" />
<CheckBox
android:id="@+id/checkboxtennis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@id/checkboxfootball"
android:layout_below="@id/checkboxfootball"
android:layout_toRightOf="@id/checkboxbasketball"
android:text="@string/tennis" />
</RelativeLayout>
<!-- 城市 -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/city"
android:layout_width="@dimen/TextViewWidth"
android:layout_height="wrap_content"
android:text="@string/cityString"
android:textSize="@dimen/fontsize" />
<Spinner
android:id="@+id/cityItems"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/city"/>
</RelativeLayout>
<!-- 注册 -->
<Button
android:id="@+id/registerButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/registerbutton"/>
</LinearLayout>
4、编辑LoginActivity.java文件
有两种方法使用字符串数组
① 在LoginActivity.java的onCreate()方法开头定义一个静态常量字符数组,文中注释的部分
② 把字符串定义在arrays.xml文件中
package com.sharpandroid.userlogin;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
public class LoginActivity extends Activity
{
/** Called when the activity is first created. */
// 定义静态常量字符数组
// private static final String[] cities =
// { "",
// "北京",
// "上海",
// "广州",
// "深圳"
// };
// 定义控件资源
private EditText name, age, passwd;
private Button regButton;
private RadioGroup sexRadioGroup;
private CheckBox basketball, football, pingpang, tennis;
private Spinner cityItem;
private boolean flag = true;
private List<CheckBox> favorities;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 定义一个ArrayList,用于存放所有的CheckBox
favorities = new ArrayList<CheckBox>();
// 获取控件资源
name = (EditText) findViewById(R.id.namevalue);
age = (EditText) findViewById(R.id.agevalue);
passwd = (EditText) findViewById(R.id.passvalue);
regButton = (Button) findViewById(R.id.registerButton);
cityItem = (Spinner) findViewById(R.id.cityItems);
sexRadioGroup = (RadioGroup) findViewById(R.id.sexMenu);
basketball = (CheckBox) findViewById(R.id.checkboxbasketball);
football = (CheckBox) findViewById(R.id.checkboxfootball);
pingpang = (CheckBox) findViewById(R.id.checkboxpingpang);
tennis = (CheckBox) findViewById(R.id.checkboxtennis);
// 将多选框添加到List中
favorities.add(basketball);
favorities.add(football);
favorities.add(pingpang);
favorities.add(tennis);
// 创建一个数组型适配器
// 用静态常量字符数组时,用这种方法
// ArrayAdapter<String> adapter = new ArrayAdapter<String>(
// LoginActivity.this, android.R.layout.simple_spinner_item, cities);
// 用资源文件中的字符数值时,用这种方法
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.cities, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cityItem.setAdapter(adapter);
regButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
flag = addUser();
if (flag)
{
new AlertDialog.Builder(LoginActivity.this)
.setTitle("请确认信息")
.setCancelable(false)
.setMessage(
"你的信息如下:" + "\n" + "姓名:"
+ name.getText().toString() + "\n"
+ "年龄:" + age.getText().toString()
+ "\n" + "性别:" + getSex() + "\n"
+ "爱好:" + getFavorite() + "\n"
+ "城市:" + getCity() + "\n")
.setPositiveButton("确定",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(
DialogInterface dialog,
int which)
{
// TODO Auto-generated method stub
ProgressDialog.show(
LoginActivity.this, "注册中",
"请等待......");
}
})
.setNegativeButton("修改",
new DialogInterface.OnClickListener()
{
@Override
public void onClick(
DialogInterface dialog,
int which)
{
// TODO Auto-generated method stub
dialog.cancel();
}
})
.show();
}
}
});
}
/*
* ==========================================================================
*/
// 获取城市
private String getCity()
{
// 引用资源文件中的字符数组时用这种方法
Resources res = getResources();
String[] cities = res.getStringArray(R.array.cities);
return cities[cityItem.getSelectedItemPosition()];
// return cities[cityItem.getSelectedItemPosition()]; // 用静态字符数组实现时,用这种方法
}
/*
* ==========================================================================
* ==================
*/
// 获取爱好
private String getFavorite()
{
String favString = "";
for (CheckBox cb : favorities)
{
if (cb.isChecked())
{
favString += cb.getText().toString();
favString += ",";
}
}
if (favString != "")
{
favString = favString.substring(0, favString.length() - 1);
} else
{
favString = "你没有选择爱好!";
}
return favString;
}
/*
* ==========================================================================
* ==================
*/
// 获取性别
private String getSex()
{
RadioButton mRadio = (RadioButton) findViewById(sexRadioGroup
.getCheckedRadioButtonId());
return mRadio.getText().toString();
}
/*
* ==========================================================================
* ==================
*/
// 添加用户
public boolean addUser()
{
boolean nameflag = true;
boolean ageflag = true;
boolean passwdflag = true;
// 判断姓名是否合法:输入的字符串姓名去掉空格后长度不能为0
if (name.getText().toString().trim().length() == 0)
{
// name.setError("用户名不能为空!");
nameflag = false;
}
if (age.getText().toString().length() == 0)
{
// age.setError("年龄不能为空!");
ageflag = false;
}
if (passwd.getText().toString().length() == 0)
{
// passwd.setError("密码不能为空!");
passwdflag = false;
}
if (nameflag && ageflag && passwdflag)
{
return true;
} else
{
if (!nameflag)
name.setError("用户名不能为空!");
if (!ageflag)
age.setError("年龄不能为空!");
if (!passwdflag)
passwd.setError("密码不能为空!");
return false;
}
}
}
三、运行界面
![运行界面][3]