2014-11-23 17:32 17人阅读 评论(0) 收藏 编辑 删除
android之实现注册功能:
注意:(1、之前一直找不到不成功 的原因,经过调试,找到了,原来是:Toast.makeText(this, "用户名不能为空", 2000).show()没调用show方法;2、在switch下的case语句后一定要加break,不然他就会一直执行下一个case的)
布局:layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"/>
<EditText
android:id="@+id/etUser"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入1-10个字符"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"/>
<EditText
android:id="@+id/etPassw"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入1-10个字符"
android:password="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确认密码:"/>
<EditText
android:id="@+id/etRePassw"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="请输入1-10个字符"
android:password="true"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择性别:"/>
<RadioGroup
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/rbMale"
android:text="男"
android:checked="true"/>
<RadioButton
android:id="@+id/rbfaMale"
android:text="女"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="城市:"/>
<Spinner
android:id="@+id/spCity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:entries="@array/citys"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="兴趣爱好:"/>
<RadioGroup
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<CheckBox
android:id="@+id/cbRead"
android:text="读书"
android:checked="true"/>
<CheckBox
android:id="@+id/cbTourist"
android:text="旅游"
/>
<CheckBox
android:id="@+id/cbPlayGame"
android:text="打电子"
/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_weight="1"
/>
<Button
android:id="@+id/btRegister"
android:text="注册"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/login32x32"
android:background="@drawable/btn_bg"
android:onClick="onclick"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_weight="1"
/>
<Button
android:id="@+id/btEixt"
android:text="退出"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/exit32x32"
android:background="@drawable/btn_bg"
android:onClick="onclick"
/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:text="返回"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|fill_horizontal"
android:background="@drawable/btn_bg"
/>
</LinearLayout>
</LinearLayout>
java代码 :
1、主要代码 :
package com.sxt.main;
import com.sxt.entity.User;
import com.sxt.main.R;
import android.text.TextUtils;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText etUser,etPassw,etRePassw;
RadioButton rbMale,rbfaMale;
CheckBox cbRead,cbTourist,cbPlayGame;
Spinner spCity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
// TODO Auto-generated method stub
etUser = (EditText) findViewById(R.id.etUser);
etPassw = (EditText) findViewById(R.id.etPassw);
etRePassw = (EditText) findViewById(R.id.etRePassw);
rbMale = (RadioButton) findViewById(R.id.rbMale);
rbfaMale = (RadioButton) findViewById(R.id.rbfaMale);
cbRead = (CheckBox) findViewById(R.id.cbRead);
cbTourist = (CheckBox) findViewById(R.id.cbTourist);
cbPlayGame = (CheckBox) findViewById(R.id.cbPlayGame);
spCity = (Spinner) findViewById(R.id.spCity);
}
public void view){
switch(view.getId()){
case R.id.btRegister:
String userName = etUser.getText().toString();
if(TextUtils.isEmpty(userName)){
Toast.makeText(this, "用户名不能为空", 2000).show();
return;
}
String passW = etPassw.getText().toString();
if(TextUtils.isEmpty(passW)){
etPassw.setError("密码不能为空");
return;
}
String rePassw = etRePassw.getText().toString();
if(TextUtils.isEmpty(rePassw)){
etRePassw.setError("确认密码不能为空");
return;
}
if(!passW.equals(rePassw)){
Toast.makeText(this, "两次密码不一致", 2000).show();
return ;
}
char sex ;
if(rbMale.isChecked()){
sex = rbMale.getText().charAt(0);
}else{
sex = rbfaMale.getText().charAt(0);
}
StringBuffer s = new StringBuffer();
if(cbRead.isChecked()){
s.append(cbRead.getText().toString()+",");
}
if(cbTourist.isChecked()){
s.append(cbTourist.getText().toString()+",");
}
if(cbPlayGame.isChecked()){
s.append(cbPlayGame.getText().toString()+",");
}
String city = spCity.getSelectedItem().toString();
User user = new User(userName,passW,sex,
s.toString().equals("")?"":s.toString().substring(0, s.length()),
city);
Toast.makeText(this, "用户注册信息:"+user.toString(),100000).show();
break;
case R.id.btEixt:
finish();
break;
}
}
}
2、user类
package com.sxt.entity;
public class User {
private String userName;
private String passWord;
private char sex;
private String city;
private String hobby;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public User(String userName, String passWord, char sex, String city,
String hobby) {
super();
this.userName = userName;
this.passWord = passWord;
this.sex = sex;
this.city = city;
this.hobby = hobby;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return this.userName+","+this.passWord+","
+this.sex+","+this.city+","+this.hobby;
}
}
效果:
本文出自 “追随心的彼岸” 博客,谢绝转载!