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 onclick(View 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;
}
}
效果:
相关推荐
android SQLite实现本地登录注册功能,SQLite简单应用(android studio)
自己实现的Android PHP JSON实现登陆注册功能,使用okhttp3实现
android实现登录注册功能,代码简单很实用
通过android studio 连接云服务器上的mysql数据库 来对mysql数据库进行操作,实现登录注册功能
android之利用SQLite数据库实现登陆和注册,使用SQLiteOpenHelper抽象类建立数据库,建立数据库类DatabaseHelper,写业务类实现查询和插入
实现连接mysql数据库,android的登录注册,包括源代码和mysql数据文件。亲测可用, 谢谢支持。
此资源实现安卓APP注册登录功能,通过Android Studio编写界面和相应功能,用Eclipse开启web服务器进行客户端与服务端之间的通信,并且将用户的账户密码经过MD5的加密后存入Mysql数据库中。
本次项目主要实现了仓库管理系统。用户分为超级管理员、出入库管理员和商品管理员这三种角色。首先进行超级管理员的注册,然后进行添加用户,超级管理员可以添加和查询其他两种用户角色。用超级管理员创建好的用户...
实现的Android客户端与php服务端交互,传输数据格式用的是json,能够从客户端想服务器的MySQL数据库中插入数据以及读取服务端的数据返回到客户端。
Android登陆 注册 源代码 运用lostview显示注册用户信息在单独的一个页面 运用SQLite执行数据库操作
Android Studio实现功能强大的购物商城APP源码,毕业设计,大作业,高分必看。现在万物互联的时代,网上shopping早已经是大家的习惯了。足不出户地选择自己想要的商品进行购买多么地方便,这就促使了这款购物商城APP...
Android SharedPreferences应用 实现本地注册登陆 功能简单易懂(实例) http://blog.csdn.net/h1028962069/article/details/9129851 文章代码
Android手机端连接Java后台 实现登录注册功能 注意修改下自己的ip地址 查询语句中修改自己的表名
本次项目主要包含了注册、登录和好友列表三个界面以及之间相互跳转。其中好友列表界面设计的很详细,有好友头像和消息内容,登录界面设计的非常好看。 打开应用,进入登录界面,用户可以点击注册按钮进入注册界面,...
加入数据库mysql实现android注册登陆功能的客户端服务器源码
使用SQLiteOnpeHelper,SQLiteDatabase实现对数据库的增删查改。以及对按钮及文本框实现获取焦点后的效果
用户注册登录的简单实现.zip