小白今天写了一段注册登录代码与大家分享不足请指出
首先写一个javabean
package com.my.domain;
public class User {private String account;//账号
private String password;//密码
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String toString() {
return "account=" + account + ", password=" + password ;
}
public User(){
super();
}
public User(String account ,String password){
super();
this.account=account;
this.password=password;
}
}
写一个dao层
package com.my.db.service;
import com.my.domain.User;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
/**
*
*登录用户数据的操作
*
*/
public class UserService {
private DBOpenHelper helper;// 创建DBOpenHelper对象
private SQLiteDatabase db;// 创建SQLiteDatabase对象
public UserService(Context cxt){
helper=new DBOpenHelper(cxt);// 初始化DBOpenHelper对象
}
//登录用
public boolean login(String account,String psw){
db=helper.getReadableDatabase();
String sql1="select account,password from user where account=? and password=?";
Cursor cursor=db.rawQuery(sql1, new String[]{account,psw});
if(cursor.moveToFirst()==true){
cursor.close();
return true;
}
return false;
}
//注册用
public boolean register(User user){
db=helper.getWritableDatabase();
String sql= "insert into user(account,password) values(?,?)";
Object obj[]={user.getAccount().toString(),user.getPassword().toString()};
db.execSQL(sql, obj);
return true;
}
//检查用户名是否存在
public boolean checkAccount(String account){
db=helper.getReadableDatabase();
String query="select * from user where account=? ";
Cursor cursor=db.rawQuery(query, new String[]{account});
if(cursor.moveToNext()){
cursor.close();
return true;
}
cursor.close();
return false;
}
}
注册页面
package com.activity;
import com.my.db.service.DBOpenHelper;
import com.my.db.service.UserService;
import com.my.domain.User;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
*
* @author 注册页面
*
*/
public class Register extends Activity {
public EditText enter_account, enter_psw, confirm_psw;
public String psw1;
public String psw2;
public String account;
UserService uService = new UserService(this);
User user = new User();
protected void onCreate(Bundle saBundle) {
super.onCreate(saBundle);
setContentView(R.layout.register);
// 获取输入账号id
enter_account = (EditText) findViewById(R.id.register_enter_account);
// 获取输入密码id
enter_psw = (EditText) findViewById(R.id.register_enter_psw);
// 获取确认密码id
confirm_psw = (EditText) findViewById(R.id.register_confirm_psw);
// 获取注册按钮
Button sign_up = (Button) findViewById(R.id.sign_up);
// 添加事件
sign_up.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
account = enter_account.getText().toString().trim();
psw1 = enter_psw.getText().toString().trim();
psw2 = confirm_psw.getText().toString().trim();
// 输入账号或者密码为空时候不能登录
if ("".equals(account) || "".equals(psw1)) {
new AlertDialog.Builder(Register.this).setTitle("错误").setMessage("帐号或密码不能空")
.setPositiveButton("确定", null).show();
}
// 判断输入账号是否存在
else if (uService.checkAccount(account)) {
Toast.makeText(Register.this, "该用户名已被注册", Toast.LENGTH_SHORT).show();
enter_account.setText("");
}
// 如果两次输入密码一样就插入数据,
else if (psw1.equals(psw2)) {
user.setAccount(account);
user.setPassword(psw1);
uService.register(user);
Toast.makeText(Register.this, "注册成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Register.this, Login.class);
startActivity(intent);
} else {
Toast.makeText(Register.this, "注册失败,两次输入密码不一样", Toast.LENGTH_SHORT).show();
enter_psw.setText("");
confirm_psw.setText("");
}
}
});
// 获取重置按钮并且添加事件
Button cz = (Button) findViewById(R.id.cz);
cz.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
enter_account.setText("");
enter_psw.setText("");
confirm_psw.setText("");
}
});
}
}
登录页面
package com.activity;
import com.my.db.service.UserService;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* 、
*
* @author登录页面
*
*/
public class Login extends Activity {
public EditText login_edit_account, login_edit_pwd;
public String edit_account;
public String edit_pwd;
UserService uService = new UserService(Login.this);
protected void onCreate(Bundle saBundle) {
super.onCreate(saBundle);
setContentView(R.layout.login);
// 获取注册按钮并为其添加事件
Button register = (Button) findViewById(R.id.register);
register.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// 点击按钮 new一个意图 打开注册页面
Intent intent1 = new Intent(Login.this, Register.class);
startActivity(intent1);
}
});
login_edit_account = (EditText) findViewById(R.id.login_edit_account);
login_edit_pwd = (EditText) findViewById(R.id.login_edit_pwd);
// 获取登录按钮并为其绑定事件
Button login = (Button) findViewById(R.id.login);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
edit_account = login_edit_account.getText().toString().trim();
edit_pwd = login_edit_pwd.getText().toString().trim();
//判断输入账号或者密码是否为空
if (edit_account.equals("") || edit_pwd.equals("")) {
new AlertDialog.Builder(Login.this).setTitle("错误").setMessage("账号或密码不能为空")
.setPositiveButton("确定", null).show();
}
// 对输入的值进行判断,调用UserService login函数
else if (uService.login(edit_account, edit_pwd)) {
Toast.makeText(Login.this, "登录成功", Toast.LENGTH_SHORT).show();
Intent intent2 = new Intent(Login.this, MainActivity.class);
startActivity(intent2);
} else {
Toast.makeText(Login.this, "登录失败,输入账号或者密码错误,请重新输入!", Toast.LENGTH_SHORT).show();
login_edit_account.setText("");
login_edit_pwd.setText("");
}
}
});
}
}
login.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"
android:background="@drawable/login_bj"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10px"
android:paddingTop="150px" >
<!-- 登录左边logo -->
<ImageView
android:id="@+id/faceImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dl_2" />
<EditText
android:id="@+id/login_edit_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/faceImg"
android:hint="输入帐号"
android:paddingLeft="45sp"
android:textColor="#ff0000" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/login_edit_account"
android:layout_alignLeft="@+id/login_edit_account"
android:layout_alignTop="@+id/login_edit_account"
android:layout_marginRight="15sp"
android:gravity="center_vertical"
android:paddingLeft="7sp"
android:text="帐号:"
android:textSize="10dp" />
<EditText
android:id="@+id/login_edit_pwd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/login_edit_account"
android:layout_alignRight="@+id/login_edit_account"
android:layout_below="@+id/login_edit_account"
android:hint="输入密码"
android:paddingLeft="45sp"
android:inputType="textPassword"
android:textColor="#ff0000" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/login_edit_pwd"
android:layout_alignLeft="@+id/login_edit_pwd"
android:layout_alignTop="@+id/login_edit_pwd"
android:layout_marginRight="15.0sp"
android:gravity="center_vertical"
android:paddingLeft="7sp"
android:text="密码:"
android:textSize="10dp"
/>
<Button
android:id="@+id/register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_below="@+id/login_edit_pwd"
android:layout_alignLeft="@+id/login_edit_pwd"
android:text="注 册"
android:textColor="#FF0000" />
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/login_edit_pwd"
android:layout_toRightOf="@id/register"
android:text="登 录"
android:textColor="#FF0000" />
</RelativeLayout>
</LinearLayout>
register.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- 用户注册页面 -->
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tablayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/login_bj" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/sign1" />
<TableRow
android:id="@+id/tablerow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="账 号:"
android:textSize="15sp" />
<EditText
android:id="@+id/register_enter_account"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="请输入账号"
android:singleLine="true" />
</TableRow>
<TableRow
android:id="@+id/tablerow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="密 码:"
android:textSize="15sp" />
<EditText
android:id="@+id/register_enter_psw"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="请输入密码"
android:inputType="textPassword"
android:singleLine="true" />
</TableRow>
<TableRow
android:id="@+id/tablerow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确认密码:"
android:textSize="15sp" />
<EditText
android:id="@+id/register_confirm_psw"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="请确认密码"
android:inputType="textPassword"
android:singleLine="true" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/sign_up"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:text="注 册"
android:textColor="#FF0000" />
<Button
android:id="@+id/cz"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="重 置"
android:textColor="#FF0000" />
</TableRow>
</TableLayout>
效果图