刚开始学习Android,希望童鞋们不吝赐教,
目前只完成界面跟验证部分,未跟数据库打交道。
[img]http://dl.iteye.com/upload/picture/pic/96406/5904d5ef-82b6-33fe-8cc4-c00dc936afd0.png[/img]
目前只完成界面跟验证部分,未跟数据库打交道。
[img]http://dl.iteye.com/upload/picture/pic/96406/5904d5ef-82b6-33fe-8cc4-c00dc936afd0.png[/img]
package com.sharpandroid;
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.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
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;
import android.widget.Toast;
public class UserRegisterActivity extends Activity {
// private static final String[] CITY = {"北京", "上海", "深圳", "合肥", "武汉"};
private int[] userHobbyId = {R.id.pingpang, R.id.basketball, R.id.football, R.id.tetennis};
private static final String TAG = "Register";
// 得到相应的显示空间对象
private EditText userName;
private EditText password;
private EditText userAge;
private RadioButton userSex;
private List<String> userHobby;
private Spinner userCity;
private Button registerButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 给Spinner初始化数据
Spinner spinner1 = (Spinner)findViewById(R.id.userCitySpinner);
ArrayAdapter<?> adapter = ArrayAdapter.createFromResource(this, R.array.city, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
userName = (EditText)findViewById(R.id.userNameEdit);
password = (EditText)findViewById(R.id.passwordEdit);
userAge = (EditText)findViewById(R.id.userAgeEdit);
// userSex = getUserSex((RadioGroup)findViewById(R.id.userSexRadioGroup));
// userHobby = getUserHobby();
// userCity = (Spinner)findViewById(R.id.userCitySpinner);
registerButton = (Button)findViewById(R.id.registerButton);
registerButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if (validationUser()) {
// Toast.makeText(UserRegisterActivity.this, "确认注册吗?", Toast.LENGTH_SHORT).show();
new AlertDialog
.Builder(UserRegisterActivity.this)
.setTitle("请确认信息")
.setMessage(registerInfo())
.setCancelable(false)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
ProgressDialog.show(UserRegisterActivity.this, "用户信息注册中...", "请等待...").setCancelable(true);
}
})
.setNegativeButton("修改", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.show();
}
}
});
/*
// 这部分的功能是想动态的生成checkbox在线性布局内
String[] hobby = getResources().getStringArray(R.array.hobby);
for (String h : hobby) {
CheckBox checkbox = new CheckBox(UserRegisterActivity.this);
Log.i(TAG, h);
}*/
// 若是ListView可以加OnItemClickListener
/* spinner1.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(context, view.getTag().toString(), Toast.LENGTH_SHORT).show();
}
});*/
}
private RadioButton getUserSex(RadioGroup radioGroup) {
return (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
}
// 获取选中的用户爱好
private List<String> getUserHobby() {
if (userHobbyId!=null&&userHobbyId.length>0) {
userHobby = new ArrayList<String>();
// StringBuilder hobby = new StringBuilder();
for (int id : userHobbyId) {
CheckBox cb = (CheckBox)findViewById(id);
if (cb.isChecked()) {
userHobby.add(cb.getText().toString());
}
}
return userHobby;
}
return null;
}
private boolean validationUser() {
if (userName==null||userName.getText().toString().length()==0) {
userName.setError("用户名不能为空!");
return false;
}
if (password==null||password.getText().toString().length()==0) {
password.setError("密码不能为空!");
return false;
}
if (userAge==null||userAge.getText().toString().length()==0) {
userAge.setError("年龄不能为空!");
return false;
} else {
String regx = "([1-9]{1})|([1-9]{1}[0-9]{1})|(1{1}0{2})";
String age = userAge.getText().toString();
if (!age.matches(regx)) {
userAge.setError("年龄应该在0-100之间");
return false;
}
}
if (getUserHobby()==null||userHobby.isEmpty()) {
Toast.makeText(UserRegisterActivity.this, "至少选择一个喜好!", Toast.LENGTH_SHORT).show();
return false;
}
userCity = (Spinner)findViewById(R.id.userCitySpinner);
userSex = getUserSex((RadioGroup)findViewById(R.id.userSexRadioGroup));
return true;
}
private String registerInfo() {
StringBuilder info = new StringBuilder();
info.append("您注册的信息如下:\n");
info.append("姓名:");info.append(userName.getText().toString());info.append("\n");
info.append("密码:");info.append(password.getText().toString());info.append("\n");
info.append("年龄:");info.append(userAge.getText().toString());info.append("\n");
info.append("性别:");info.append(userSex.getText().toString());info.append("\n");
info.append("爱好:");info.append(userHobby);info.append("\n");
info.append("城市:");info.append(userCity.getSelectedItem().toString());info.append("\n");
Log.i(TAG, info.toString());
return info.toString();
}
}