这个数据库的基本使用上篇中已经讲过了,今天就是怎么实现简单的本地注册登录等功能
不啰嗦,直接上代码:
1、bean类
package com.sdp.panda.pictrueapp.bean;
/**
* Created by 80926 on 2016/10/20.
*/
public class UserBean {
private String name;
private String password;
private int id;
public String getName () {
return name;
}
public void setName (String name) {
this .name = name;
}
public String getPassword () {
return password;
}
public void setPassword (String password) {
this .password = password;
}
public int getId () {
return id;
}
public void setId (int id) {
this .id = id;
}
@Override
public String toString () {
return "UserBean{" +
"name='" + name + '\'' +
", password='" + password + '\'' +
", id=" + id +
'}' ;
}
}
2、三个布局文件
1.activity_forget.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="match_parent"
android:layout_height ="match_parent" >
<EditText
android:id ="@+id/et_forget_name"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:drawableLeft ="@mipmap/biaoqingbao"
android:layout_marginTop ="164dp"
android:layout_alignParentTop ="true"
android:layout_alignParentLeft ="true"
android:layout_alignParentStart ="true"
android:background ="@null"
android:hint ="输入用户名" />
<EditText
android:id ="@+id/et_forget_password"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:drawableLeft ="@mipmap/hongbao"
android:hint ="输入密码"
android:background ="@null"
android:layout_below ="@+id/et_forget_name"
android:layout_alignParentLeft ="true"
android:layout_alignParentStart ="true"
android:layout_marginTop ="14dp" />
<EditText
android:id ="@+id/et_re_forget_password"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:drawableLeft ="@mipmap/hongbao"
android:hint ="再输入一遍"
android:background ="@null"
android:layout_below ="@+id/et_forget_password"
android:layout_alignParentLeft ="true"
android:layout_alignParentStart ="true"
android:layout_marginTop ="18dp" />
<Button
android:id ="@+id/btn_forget"
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
android:text ="更改密码"
android:textColor ="#ff00"
android:layout_below ="@+id/et_re_forget_password"
android:layout_alignParentLeft ="true"
android:layout_alignParentStart ="true"
android:layout_marginTop ="10dp" />
</RelativeLayout >
2.activity_user.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="match_parent"
android:layout_height ="match_parent" >
<EditText
android:id ="@+id/et_name"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:drawableLeft ="@mipmap/biaoqingbao"
android:layout_marginTop ="164dp"
android:layout_alignParentTop ="true"
android:layout_alignParentLeft ="true"
android:layout_alignParentStart ="true"
android:background ="@null"
android:hint ="输入用户名" />
<EditText
android:id ="@+id/et_password"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:drawableLeft ="@mipmap/hongbao"
android:hint ="输入密码"
android:background ="@null"
android:layout_below ="@+id/et_name"
android:layout_alignParentLeft ="true"
android:layout_alignParentStart ="true"
android:layout_marginTop ="14dp" />
<Button
android:id ="@+id/btn_login"
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
android:text ="登录"
android:textColor ="#ff00"
android:layout_below ="@+id/et_password"
android:layout_alignParentLeft ="true"
android:layout_alignParentStart ="true"
android:layout_marginTop ="10dp" />
<TextView
android:id ="@+id/tv_forget"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="忘记密码"
android:textSize ="18sp"
android:textColor ="#00ff00"
android:layout_below ="@+id/btn_login"
android:layout_alignParentLeft ="true"
android:layout_alignParentStart ="true"
android:layout_marginTop ="12dp" />
<TextView
android:id ="@+id/tv_resiger"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="注册新用户"
android:textSize ="18sp"
android:textColor ="#00ff00"
android:layout_alignBaseline ="@+id/tv_forget"
android:layout_alignBottom ="@+id/tv_forget"
android:layout_alignParentRight ="true"
android:layout_alignParentEnd ="true" />
</RelativeLayout >
3.activity_resgiter.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="match_parent"
android:layout_height ="match_parent" >
<EditText
android:id ="@+id/et_res_name"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:drawableLeft ="@mipmap/biaoqingbao"
android:layout_marginTop ="164dp"
android:layout_alignParentTop ="true"
android:layout_alignParentLeft ="true"
android:layout_alignParentStart ="true"
android:background ="@null"
android:hint ="输入用户名" />
<EditText
android:id ="@+id/et_res_password"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:drawableLeft ="@mipmap/hongbao"
android:hint ="输入密码"
android:background ="@null"
android:layout_below ="@+id/et_res_name"
android:layout_alignParentLeft ="true"
android:layout_alignParentStart ="true"
android:layout_marginTop ="14dp" />
<EditText
android:id ="@+id/et_re_res_password"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:drawableLeft ="@mipmap/hongbao"
android:hint ="再输入一遍"
android:background ="@null"
android:layout_below ="@+id/et_res_password"
android:layout_alignParentLeft ="true"
android:layout_alignParentStart ="true"
android:layout_marginTop ="18dp" />
<Button
android:id ="@+id/btn_res_resigter"
android:layout_width ="match_parent"
android:layout_height ="wrap_content"
android:text ="注册"
android:textColor ="#ff00"
android:layout_below ="@+id/et_re_res_password"
android:layout_alignParentLeft ="true"
android:layout_alignParentStart ="true"
android:layout_marginTop ="10dp" />
</RelativeLayout >
3、三个Activity
1.UserActivity
package com.sdp.panda.pictrueapp;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.lidroid.xutils.db.sqlite.Selector;
import com.lidroid.xutils.exception.DbException;
import com.sdp.panda.pictrueapp.app.MyApp;
import com.sdp.panda.pictrueapp.bean.UserBean;
/**
* Created by 80926 on 2016/10/20.
*/
public class UserActivity extends Activity implements View .OnClickListener {
private Button btnLogin;
private TextView tvResig;
private TextView tvForget;
private EditText etPassword;
private EditText etName;
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_uesr);
initData();
initView();
}
private void initData () {
}
private void initView () {
etName = (EditText) findViewById(R.id.et_name);
etPassword = (EditText) findViewById(R.id.et_password);
tvForget = (TextView) findViewById(R.id.tv_forget);
tvResig = (TextView) findViewById(R.id.tv_resiger);
btnLogin = (Button) findViewById(R.id.btn_login);
btnLogin.setOnClickListener(this );
tvForget.setOnClickListener(this );
tvResig.setOnClickListener(this );
}
@Override
public void onClick (View v) {
switch (v.getId()){
case R.id.btn_login:
String name = etName.getText().toString().trim();
String password= etPassword.getText().toString().trim();
try {
UserBean userFindName = MyApp.dbUtils.findFirst(Selector.from(UserBean.class).where("name" , "=" , name));
if (userFindName==null ){
Toast.makeText(this , "用户名不存在" , Toast.LENGTH_SHORT).show();
etName.setText("" );
etPassword.setText("" );
}else {
String userFindPassword = userFindName.getPassword();
if (!userFindPassword.equals(password)){
Toast.makeText(this , "密码错误,请重新输入" , Toast.LENGTH_SHORT).show();
SharedPreferences.Editor edit = MyApp.sp.edit();
edit.putString("name" ,name);
edit.commit();
etPassword.setText("" );
}else {
Toast.makeText(this , "登陆成功" , Toast.LENGTH_SHORT).show();
startActivity(new Intent(this ,InfoActivity.class));
}
}
} catch (DbException e) {
e.printStackTrace();
}
break ;
case R.id.tv_forget:
startActivity(new Intent(this ,ForgetActivity.class));
break ;
case R.id.tv_resiger:
Intent intent = new Intent(this ,ResigterActivity.class);
startActivity(intent);
break ;
}
}
@Override
protected void onDestroy () {
super .onDestroy();
}
}
2.ResigterActivity
package com .sdp .panda .pictrueapp
import android.app .Activity
import android.os .Bundle
import android.view .View
import android.widget .Button
import android.widget .EditText
import android.widget .Toast
import com .lidroid .xutils .db .sqlite .Selector
import com .lidroid .xutils .exception .DbException
import com .sdp .panda .pictrueapp .app .MyApp
import com .sdp .panda .pictrueapp .bean .UserBean
public class ResigterActivity extends Activity implements View.OnClickListener {
private Button btnLogin
private EditText etPassword,etRePassword
private EditText etName
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState)
setContentView(R.layout .activity _resigter)
initView()
}
private void initView() {
etName = (EditText) findViewById(R.id .et _res_name)
etPassword = (EditText) findViewById(R.id .et _res_password)
etRePassword = (EditText) findViewById(R.id .et _re_res_password)
btnLogin = (Button) findViewById(R.id .btn _res_resigter)
btnLogin.setOnClickListener (this)
}
@Override
public void onClick(View v) {
String name = etName.getText ().toString ().trim ()
String password = etPassword.getText ().toString ().trim ()
String rePassword = etRePassword.getText ().toString ().trim ()
if (!name.equals ("" )&&!password.equals ("" )&&!rePassword.equals ("" )){
if (password.equals (rePassword)){
UserBean userBean = new UserBean()
userBean.setName (name)
userBean.setPassword (password)
try {
UserBean userFindName = MyApp.dbUtils .findFirst (Selector.from (UserBean.class ).where ("name" , "=" , name))
if (userFindName==null) {
MyApp.dbUtils .save (userBean)
Toast.makeText (this, "注册成功" , Toast.LENGTH _SHORT).show ()
finish()
}else {
Toast.makeText (this, "用户名已注册,重新输入" , Toast.LENGTH _SHORT).show ()
etName.setText ("" )
etPassword.setText ("" )
etRePassword.setText ("" )
}
} catch (DbException e) {
e.printStackTrace ()
}
}else {
Toast.makeText (this, "两次密码不一致" , Toast.LENGTH _SHORT).show ()
etPassword.setText ("" )
etRePassword.setText ("" )
}
}else {
Toast.makeText (this, "不能为空" , Toast.LENGTH _SHORT).show ()
etRePassword.setText ("" )
etPassword.setText ("" )
etName.setText ("" )
}
}
}
3.ForgetActivity
package com .sdp .panda .pictrueapp
import android.app .Activity
import android.content .SharedPreferences
import android.os .Bundle
import android.view .View
import android.widget .Button
import android.widget .EditText
import android.widget .Toast
import com .lidroid .xutils .db .sqlite .Selector
import com .lidroid .xutils .exception .DbException
import com .sdp .panda .pictrueapp .app .MyApp
import com .sdp .panda .pictrueapp .bean .UserBean
public class ForgetActivity extends Activity implements View.OnClickListener {
private Button btnLogin
private EditText etPassword,etRePassword
private EditText etName
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState)
setContentView(R.layout .activity _forget)
initView()
}
private void initView() {
etName = (EditText) findViewById(R.id .et _forget_name)
etPassword = (EditText) findViewById(R.id .et _forget_password)
etRePassword = (EditText) findViewById(R.id .et _re_forget_password)
btnLogin = (Button) findViewById(R.id .btn _forget)
etName.setText (MyApp.sp .getString ("name" ,"" ))
btnLogin.setOnClickListener (this)
}
@Override
public void onClick(View v) {
String name = etName.getText ().toString ().trim ()
String password = etPassword.getText ().toString ().trim ()
String rePassword = etRePassword.getText ().toString ().trim ()
if (!name.equals ("" )&&!password.equals ("" )&&!rePassword.equals ("" )){
try {
UserBean userFindName = MyApp.dbUtils .findFirst (Selector.from (UserBean.class ).where ("name" , "=" , name))
if(userFindName == null){
Toast.makeText (this, "用户名不存在" , Toast.LENGTH _SHORT).show ()
etRePassword.setText ("" )
etPassword.setText ("" )
etName.setText ("" )
}else {
if (password.equals (rePassword)) {
MyApp.dbUtils .delete (userFindName)
UserBean userBean = new UserBean()
userBean.setPassword (password)
userBean.setName (name)
MyApp.dbUtils .save (userBean)
Toast.makeText (this, "密码更改完成" , Toast.LENGTH _SHORT).show ()
finish()
}else {
Toast.makeText (this, "两次密码不一致" , Toast.LENGTH _SHORT).show ()
etPassword.setText ("" )
etRePassword.setText ("" )
}
}
} catch (DbException e) {
e.printStackTrace ()
}
}else {
Toast.makeText (this, "不能为空" , Toast.LENGTH _SHORT).show ()
etName.setText ("" )
etPassword.setText ("" )
etRePassword.setText ("" )
}
}
}