基于SQLite的Android登录APP

基于SQLite的Android登录APP

该登录APP主要包括三个模块:

1、登录:用户选择登录方式、登录身份,输入账号密码,完成登录。
2、忘记密码:用户输入新密码及验证码修改登录密码。
3、个人信息:用户完成登录后设置个人信息并显示。

使用控件:

1、单选按钮RadioButton:区分是密码登录还是验证码登录。
2、下拉框Spinner:区分是个人用户还是公司用户。
3、编辑框EditText:输入手机号和密码(或验证码)。
4、复选框CheckBox:判断是否记住密码。
5、相对布局RelativeLayout:界面的整体布局,方便将各个控件按照相对位置摆放。
6、框架布局FrameLayout:在框架布局中后面添加的子视图会把之前的子视图覆盖掉,一般用于需要重叠显示的场合。用于实现忘记密码按钮和密码输入框的叠加。

采用的存储方式

1、共享参数SharedPreferences:

是Android的一个轻量级存储工具,采用的存储结构是Key-Value的键值对方式,类似于Java的Properties类,都是把Key-Value的键值对保存在配置文件中,不同的是Properties的文件内容是Key=Value的形式,而SharedPreferences的存储介质是符合XML规范的配置文件。本案例中用于保存用户的账号和密码。

2、数据库SQLite:

是一个小巧的嵌入式数据库。本案例中用于存储用户的个人信息。

成果展示:

界面设计:

1. 登录界面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".LoginActivity"
 android:paddingTop="10dp"
 android:padding="8dp">

 <RadioGroup
 android:id="@+id/rg_login_way"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal"
 android:layout_marginTop="20dp">

 <RadioButton
  android:id="@+id/rb_password"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="密码登录"
  android:textSize="25sp" />
 <RadioButton
  android:id="@+id/rb_checkcode"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="验证码登录"
  android:layout_marginLeft="50dp"
  android:textSize="25sp" />

 </RadioGroup>
 <TextView
 android:id="@+id/tv_shenfen"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="我是:"
 android:layout_below="@+id/rg_login_way"
 android:textSize="25sp"
 android:layout_marginTop="40dp"
 android:textColor="@color/black"
 android:layout_marginLeft="30dp"
 android:layout_alignRight="@+id/tv_phonenum"/>
 <Spinner
 android:id="@+id/sp_shenfen"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/tv_shenfen"
 android:layout_alignBottom="@+id/tv_shenfen"

 android:spinnerMode="dialog"/>

 <TextView
 android:id="@+id/tv_phonenum"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/tv_shenfen"
 android:text="手机号码:"
 android:textSize="25sp"
 android:textColor="@color/black"
 android:layout_marginTop="40dp"/>
 <EditText
 android:id="@+id/et_phone"
 android:layout_width="match_parent"
 android:layout_height="50dp"
 android:layout_alignBaseline="@id/tv_phonenum"
 android:layout_toRightOf="@+id/tv_phonenum"
 android:background="@drawable/eb_selector"
 android:textSize="25sp"
 android:hint="请输入手机号码"
 android:inputType="number" />

 <TextView
 android:id="@+id/tv_psw"
 android:layout_width="wrap_content"
 android:layout_height="40dp"
 android:text="登录密码:"
 android:layout_below="@id/tv_phonenum"
 android:textSize="25sp"
 android:layout_marginTop="40dp"
 android:textColor="@color/black"/>
 <FrameLayout
 android:id="@+id/fm_psw"
 android:layout_width="match_parent"
 android:layout_height="50dp"
 android:layout_toRightOf="@id/tv_psw"
 android:layout_alignBottom="@+id/tv_psw"
 android:layout_alignLeft="@+id/et_phone">

 <EditText
  android:id="@+id/et_psw"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:hint="请输入密码"
  android:textSize="25sp"
  android:background="@drawable/eb_selector" />
 <Button
  android:id="@+id/btn_pswforget"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:text="忘记密码"
  android:textSize="25sp"
  android:background="@color/darkgray"
  android:padding="10dp"
  android:layout_gravity="end"/>
 </FrameLayout>

 <CheckBox
 android:id="@+id/cb_pswrmb"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_below="@+id/tv_psw"
 android:text="记住密码"
 android:textSize="25sp"
 android:layout_marginTop="30dp"/>
 <Button
 android:id="@+id/btn_login"
 android:layout_width="match_parent"
 android:layout_height="60dp"
 android:layout_below="@id/cb_pswrmb"
 android:text="登录"
 android:textSize="25sp"
 android:layout_marginTop="30dp"
 android:background="@color/darkgray"/>

</RelativeLayout>

2.忘记密码界面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:padding="8dp"
 android:paddingTop="10dp"
 tools:context=".PswForgetActivity">
 <TextView
 android:id="@+id/tv_newpsw"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="输入新密码:"
 android:textColor="@color/black"
 android:textSize="25sp"
 android:layout_marginTop="20dp"/>
 <EditText
 android:id="@+id/et_newpsw"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/tv_newpsw"
 android:background="@drawable/eb_selector"
 android:layout_alignBaseline="@+id/tv_newpsw"
 android:hint="请输入新密码"
 android:textSize="25sp"
 android:inputType="textPassword"
 />
 <TextView
 android:id="@+id/tv_chknewpsw"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="40dp"
 android:text="确认新密码:"
 android:layout_below="@+id/tv_newpsw"
 android:textSize="25sp"
 android:textColor="@color/black"/>
 <EditText
 android:id="@+id/et_chknewpsw"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/tv_chknewpsw"
 android:layout_alignBaseline="@+id/tv_chknewpsw"
 android:background="@drawable/eb_selector"
 android:textSize="25sp"
 android:hint="请再次输入新密码"
 android:inputType="textPassword"/>
 <TextView
 android:id="@+id/tv_checkcode"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="验证码:"
 android:layout_below="@+id/tv_chknewpsw"
 android:textSize="25sp"
 android:textColor="@color/black"
 android:layout_marginTop="40dp"/>
 <FrameLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/tv_checkcode"
 android:layout_below="@+id/et_chknewpsw"
 android:layout_marginTop="20dp">
 <EditText
  android:id="@+id/et_checkcode"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:textSize="25sp"
  android:hint="输入验证码"
  android:inputType="number"
  android:background="@drawable/eb_selector"
  android:maxLines="1"/>
 <Button
  android:id="@+id/btn_sendcheckcode"
  android:layout_width="wrap_content"
  android:layout_height="match_parent"
  android:layout_gravity="right"
  android:text="获取验证码"
  android:textSize="25sp"
  android:padding="10dp"
  android:textColor="@color/black"
  android:background="@color/darkgray"/>

 </FrameLayout>

 <Button
 android:id="@+id/btn_check"
 android:layout_width="match_parent"
 android:layout_height="60dp"
 android:layout_below="@id/tv_checkcode"
 android:text="确定"
 android:textSize="25sp"
 android:layout_marginTop="30dp"
 android:background="@color/darkgray" />

</RelativeLayout>

3.个人信息填写界面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".SharedPreferencesActivity"
 android:padding="10dp">
 <TextView
 android:id="@+id/tv_name"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="姓名:"
 android:textSize="25sp"
 android:textColor="@color/black"
 android:layout_marginTop="20dp"/>
 <EditText
 android:id="@+id/et_name"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/tv_name"
 android:layout_alignBaseline="@+id/tv_name"
 android:background="@drawable/eb_selector"
 android:maxLines="1"/>

 <TextView
 android:id="@+id/tv_age"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="年龄:"
 android:textSize="25sp"
 android:textColor="@color/black"
 android:layout_below="@+id/tv_name"
 android:layout_marginTop="20dp"/>
 <EditText
 android:id="@+id/et_age"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/tv_age"
 android:layout_alignBaseline="@+id/tv_age"
 android:background="@drawable/eb_selector"
 android:maxLines="1"
 android:inputType="number"/>
 <TextView
 android:id="@+id/tv_height"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="身高:"
 android:textSize="25sp"
 android:textColor="@color/black"
 android:layout_below="@+id/tv_age"
 android:layout_marginTop="20dp"/>
 <EditText
 android:id="@+id/et_height"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/tv_height"
 android:layout_alignBaseline="@+id/tv_height"
 android:background="@drawable/eb_selector"
 android:maxLines="1"
 android:inputType="number"/>
 <TextView
 android:id="@+id/tv_weight"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="体重:"
 android:textSize="25sp"
 android:textColor="@color/black"
 android:layout_below="@+id/tv_height"
 android:layout_marginTop="20dp"/>
 <EditText
 android:id="@+id/et_weight"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_toRightOf="@+id/tv_weight"
 android:layout_alignBaseline="@+id/tv_weight"
 android:background="@drawable/eb_selector"
 android:maxLines="1"
 android:inputType="number"/>
 <TextView
 android:id="@+id/tv_married"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="婚否:"
 android:layout_below="@+id/tv_weight"
 android:textSize="25sp"
 android:textColor="@color/black"
 android:layout_marginTop="20dp"/>
 <Spinner
 android:id="@+id/sp_married"
 android:layout_width="match_parent"
 android:layout_height="40dp"
 android:spinnerMode="dropdown"
 android:layout_toRightOf="@+id/tv_married"
 android:layout_alignBottom="@+id/tv_married"/>

 <Button
 android:id="@+id/btn_save"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/tv_married"
 android:layout_marginTop="20dp"
 android:background="@drawable/selector"
 android:text="保存"
 android:textSize="25sp"
 android:textColor="@color/black"/>

</RelativeLayout>

4.个人信息显示界面

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".SharedPreferencesActivity2">

 <TextView
 android:id="@+id/tv_show"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_below="@+id/btn_delete"
 android:textSize="25sp"/>
 <Button
 android:id="@+id/btn_delete"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="删除"
 android:textSize="25sp"/>

</RelativeLayout>

代码实现

UserDBHelper

package com.example.helloworld;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.core.app.NavUtils;

import java.util.ArrayList;
import java.util.Locale;

public class UserDBHelper extends SQLiteOpenHelper {

 private static final String TAG = "UserDBHelper";
 private static final String DB_NAME = "user.db"; //数据库名
 private static final int DB_VERSION = 1;  //数据库版本
 private static UserDBHelper mHelper = null;
 private SQLiteDatabase mDB = null;
 private static final String TABLE_NAME = "user_info"; //表名

 private UserDBHelper(Context context){
 super(context,DB_NAME,null,DB_VERSION);

 }

 private UserDBHelper(Context context,int version){
 super(context,DB_NAME,null,version);
 }

 public static UserDBHelper getInstance(Context context,int version){
 if(version > 0 && mHelper == null){
  mHelper = new UserDBHelper(context,version);
 }else if(mHelper == null){
  mHelper = new UserDBHelper(context);
 }
 return mHelper;
 }

 public SQLiteDatabase openReadLink(){
 if (mDB == null || !mDB.isOpen()){
  mDB = mHelper.getReadableDatabase();
 }
 return mDB;
 }

 public SQLiteDatabase openWriteLink(){
 if (mDB == null || !mDB.isOpen()){
  mDB = mHelper.getWritableDatabase();
  Log.d(TAG, "openWriteLink: 打开了读数据库");
 }
 return mDB;
 }

 public void closeLink(){
 if (mDB != null && mDB.isOpen()){
  mDB.close();
  mDB = null;
 }
 }

 public String getDBName(){
 if(mHelper != null){
  return mHelper.getDatabaseName();
 }else {
  return DB_NAME;
 }
 }

 @Override
 public void onCreate(SQLiteDatabase db) {
 Log.d(TAG, "onCreate: 创建数据库");
 String drop_sql = "DROP TABLE IF EXISTS " + TABLE_NAME + ";";
 db.execSQL(drop_sql);
 String create_sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ("
  + "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"
  + "name VARCHAR NOT NULL,"
  + "age INTEGER NOT NULL,"
  + "height LONG NOT NULL,"
  + "weight FLOAT NOT NULL,"
  + "married INTEGER NOT NULL,"
  + "update_time VARCHAR NOT NULL,"
  + "phone VARCHAR NOT NULL,"
  + "password VARCHAR NOT NULL"
  + ");";
 Log.d(TAG, "create_sql" + create_sql);
 db.execSQL(create_sql);
 }

 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 Log.d(TAG, "onUpgrade oldVersion=" +oldVersion+",newVersion=" + newVersion+"数据库新旧版本号");
 if (newVersion > 1){
  String alter_sql = "ALTER TABLE" + TABLE_NAME + "ADD COLUMN" + "phone VARCHAR;";
  Log.d(TAG, "alter_sql:" + alter_sql);
  db.execSQL(alter_sql);
  alter_sql = "ALTER TABLE" + TABLE_NAME + "ADD COLUMN" + "password VARCHAR;";
  Log.d(TAG, "alter_sql:" + alter_sql);
  db.execSQL(alter_sql);
 }
 }

 public int delete(String condition){
 int count = mDB.delete(TABLE_NAME,condition,null);
 return count;
 }
 public int deleteAll(){
 int count = mDB.delete(TABLE_NAME,"1=1",null);
 return count;
 }

 public long insert(UserInfo info){
 ArrayList<UserInfo> infoArray = new ArrayList<UserInfo>();
 infoArray.add(info);
 return insert(infoArray);
 }

 public ArrayList<UserInfo>query(String condition) {
 String sql = String.format(Locale.CHINA,"select rowid,_id,name,age,height,weight,married,update_time," + "phone,password from %s where %s;", TABLE_NAME,condition);
 Log.d(TAG, "query sql: " + sql);
 ArrayList<UserInfo> infoArray = new ArrayList<UserInfo>();
 Cursor cursor = mDB.rawQuery(sql, null);
 while (cursor.moveToNext()) {
  UserInfo info = new UserInfo();
  info.rowid = cursor.getLong(0);
  info.xuhao = cursor.getInt(1);
  info.name = cursor.getString(2);
  info.age = cursor.getInt(3);
  info.height = cursor.getLong(4);
  info.weight = cursor.getFloat(5);
  info.married = (cursor.getInt(6) == 0) ? false : true;
  info.update_time = cursor.getString(7);
  info.phone = cursor.getString(8);
  info.password = cursor.getString(9);
  infoArray.add(info);
 }
 cursor.close();
 return infoArray;
 }

 public long insert(ArrayList<UserInfo> infoArray) {
 long result = -1;
 for (int i = 0; i < infoArray.size(); i++) {
  UserInfo info = infoArray.get(i);
  ArrayList<UserInfo> tempArray = new ArrayList<UserInfo>();

  if (info.name != null && info.name.length() > 0) {
  String condition = String.format("name='%s'", info.name);
  tempArray = query(condition);
  if (tempArray.size() > 0) {
   update(info, condition);
   result = tempArray.get(0).rowid;
   continue;
  }
  }

  if (info.phone != null && info.phone.length() > 0) {
  String condition = String.format("phone='%s'", info.phone);
  tempArray = query(condition);
  if (tempArray.size() > 0) {
   update(info, condition);
   result = tempArray.get(0).rowid;
   continue;
  }
  }
  Log.d(TAG, "insert: 当前版本号"+mDB.getVersion());

  ContentValues cv = new ContentValues();
  cv.put("name", info.name);
  cv.put("age", info.age);
  cv.put("height", info.height);
  cv.put("weight", info.weight);
  cv.put("married", info.married);
  cv.put("update_time", info.update_time);
  cv.put("phone", info.phone);
  cv.put("password", info.password);
  result = mDB.insert(TABLE_NAME, "", cv);
  if (result == -1) {
  return result;
  }
 }
 return result;
 }

 public int update(UserInfo info, String condition) {
 ContentValues cv = new ContentValues();
 cv.put("name", info.name);
 cv.put("age", info.age);
 cv.put("height", info.height);
 cv.put("weight", info.weight);
 cv.put("married", info.married);
 cv.put("update_time", info.update_time);
 cv.put("phone", info.phone);
 cv.put("password", info.password);
 // 执行更新记录动作,该语句返回记录更新的数目
 return mDB.update(TABLE_NAME, cv, condition, null);
 }

 public int update(UserInfo info) {
 return update(info, "rowid=" + info.rowid);
 }

 public UserInfo queryByPhone(String phone){
 UserInfo info = null;
 ArrayList<UserInfo> infoArray = query(String.format("phone=%s",phone));
 if (infoArray.size() > 0 ){
  info = infoArray.get(0);
 }
 return info;
 }
}

UserInfo

package com.example.helloworld;

public class UserInfo {
 public long rowid;
 public int xuhao;
 public String name;
 public int age;
 public long height;
 public float weight;
 public boolean married;
 public String update_time;
 public String phone;
 public String password;

 public UserInfo() {
 rowid = 0l;
 xuhao = 0;
 name = "";
 age = 0;
 height = 0l;
 weight = 0.0f;
 married = false;
 update_time = "";
 phone = "";
 password = "";
 }
}

LoginActivity

package com.example.helloworld;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Rect;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import java.net.PasswordAuthentication;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
 private EditText et_phone;
 private RadioButton rb_psw;
 private RadioButton rb_checkcode;
 private EditText et_psw;
 private Button btn_pswforget;
 private String mPassword;
 private int mRequestcode;
 private String mCheckCode;
 private int mType;
 private String TAG = "huahua";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_login);
 rb_psw = findViewById(R.id.rb_password);
 rb_checkcode = findViewById(R.id.rb_checkcode);
 et_phone = findViewById(R.id.et_phone);
 et_psw = findViewById(R.id.et_psw);
 CheckBox cb_pswforget = findViewById(R.id.cb_pswrmb);
 Button btn_login = findViewById(R.id.btn_login);
 btn_pswforget = findViewById(R.id.btn_pswforget);
 btn_login.setOnClickListener(this);
 btn_pswforget.setOnClickListener(this);
 RadioGroup rg = findViewById(R.id.rg_login_way);
 mPassword = et_psw.getText().toString();

 rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
  @Override
  public void onCheckedChanged(RadioGroup group, int checkedId) {
  RadioButton rb = findViewById(checkedId);
  Log.i(TAG, "onCheckedChanged: 密码登录"+ rb_psw.isChecked());
  Log.i(TAG, "onCheckedChanged: 验证码登录"+rb_checkcode.isChecked());
  if(rb_psw.isChecked()){
   et_psw.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
  }
  }
 });

 cb_pswforget.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
  @Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

  }
 });

 ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this,R.layout.item_dropdown,typeArray);
 typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
 Spinner sp_type = findViewById(R.id.sp_shenfen);
 sp_type.setAdapter(typeAdapter);
 sp_type.setSelection(0);
 sp_type.setPrompt("选择你的登录身份:");
 sp_type.setOnItemSelectedListener(new MyOnItemSeclectedListener());
 }
 private String[] typeArray = {"个人用户","企业用户"};
 private class MyOnItemSeclectedListener implements AdapterView.OnItemSelectedListener{
 @Override
 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  mType = position;
 }

 @Override
 public void onNothingSelected(AdapterView<?> parent) {

 }
 }

 @Override
 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 if (requestCode == mRequestcode && data != null) {
  mPassword = data.getStringExtra("newpsw");
 }
 }

 @Override
 protected void onRestart() {
 et_psw.setText("");
 super.onRestart();
 }

 @Override
 public void onClick(View v) {
 String phone = et_phone.getText().toString();
 switch (v.getId()){
  case R.id.btn_pswforget: {
  Log.i(TAG, "onClick: 点击了忘记密码");
  if (phone == null || phone.length() < 11) {
   Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
   return;
  }
  if (rb_psw.isChecked()) {
   Log.i(TAG, "onClick: 进入忘记密码界面");
   Intent intent = new Intent(this, PswForgetActivity.class);
   intent.putExtra("phone", phone);
   startActivityForResult(intent, mRequestcode);
  } else if (rb_checkcode.isChecked()) {
   mCheckCode = String.format("%06d", (int) (Math.random() * 1000000 % 1000000));
   Log.i(TAG, "onClick: 发送验证码");
   AlertDialog.Builder builder = new AlertDialog.Builder(this);
   builder.setTitle("请记住验证码!");
   builder.setMessage("手机号" + phone + ",本次验证码是:" + mCheckCode + ",请输入验证码");
   builder.setPositiveButton("确定", null);
   AlertDialog alert = builder.create();
   alert.show();
  }
  }
  break;
  case R.id.btn_login: {
  if (phone == null || phone.length() < 11) {
   Toast.makeText(this, "请输入正确的手机号!", Toast.LENGTH_SHORT).show();
   Log.i(TAG, "onClick: 验证密码");
   return;
  }
  if (rb_psw.isChecked()) {
   if (!et_psw.getText().toString().equals(mPassword) || et_psw.getText().toString().equals("")) {
   Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show();
   return;
   } else {
   loginSuccess();
   }
  } else if (rb_checkcode.isChecked()) {
   if (!et_psw.getText().toString().equals(mCheckCode)) {
   Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
   return;
   } else {
   loginSuccess();
   }
  }
  }
 }
 }
 private void loginSuccess(){
 String desc = String.format("您的手机号码是%s,类型是%s。恭喜你通过登录验证,点击“确定”按钮返回上个页面",et_phone.getText().toString(),typeArray[mType]);
 AlertDialog.Builder builder = new AlertDialog.Builder(this);
 builder.setTitle("登录成功");
 builder.setMessage(desc);
 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  @Override
  public void onClick(DialogInterface dialog, int which) {
  Intent intent = new Intent(LoginActivity.this,SharedPreferencesActivity.class);
  startActivity(intent);
  SharedPreferences sps = getSharedPreferences("Login", Context.MODE_PRIVATE);
  SharedPreferences.Editor editor = sps.edit();
  editor.putString("phone",et_phone.getText().toString());
  editor.putString("password",et_psw.getText().toString());
  editor.apply();
  }
 });
 builder.setNegativeButton("取消",null);
 AlertDialog alert = builder.create();
 alert.show();
 }
}

PswForgetActivity

package com.example.helloworld;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Toast;

public class PswForgetActivity extends AppCompatActivity implements View.OnClickListener {
 private EditText et_newpsw;
 private EditText et_chknewpsw;
 private EditText et_checkcode;
 private String mCheckCode;
 private String mPhone;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_psw_forget);
 et_newpsw = findViewById(R.id.et_newpsw);
 et_chknewpsw = findViewById(R.id.et_chknewpsw);
 et_checkcode = findViewById(R.id.et_checkcode);
 findViewById(R.id.btn_sendcheckcode).setOnClickListener(this);
 findViewById(R.id.btn_check).setOnClickListener(this);
 mPhone = getIntent().getStringExtra("phone");
 et_newpsw.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
 et_chknewpsw.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
 }

 @Override
 public void onClick(View v) {
 switch (v.getId()){
  case R.id.btn_sendcheckcode:
  if(mPhone == null || mPhone.length() < 11){
   Toast.makeText(this,"请输入正确的手机号",Toast.LENGTH_SHORT).show();
   return;
  }
  mCheckCode = String.format("%06d",(int)(Math.random()*1000000%1000000));
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  builder.setTitle("请记住验证码");
  builder.setMessage("手机号"+mPhone+",本次验证码是"+mCheckCode+",请输入验证码");
  builder.setPositiveButton("确定",null);
  AlertDialog alertDialog = builder.create();
  alertDialog.show();
  case R.id.btn_check:
  String newpsw = et_newpsw.getText().toString();
  String chknewpsw = et_chknewpsw.getText().toString();
  if(newpsw == null || newpsw.length() < 6 || chknewpsw == null || chknewpsw.length() < 6){
   Toast.makeText(this,"请输入正确的新密码",Toast.LENGTH_SHORT).show();
   return;
  }else if(!newpsw.equals(chknewpsw)){
   Toast.makeText(this,"两次输入的新密码不一致",Toast.LENGTH_SHORT).show();
   return;
  }else if(!et_checkcode.getText().toString().equals(mCheckCode)){
   Toast.makeText(this,"请输入正确的验证码",Toast.LENGTH_SHORT).show();
   return;
  }else {
   Toast.makeText(this,"密码修改成功",Toast.LENGTH_SHORT).show();
   Intent intent = new Intent();
   intent.putExtra("newpsw",newpsw);
   setResult(Activity.RESULT_OK,intent);
   finish();
  }
 }
 }
}

InfoWriteActivity

package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class InfoWriteActivity extends AppCompatActivity implements View.OnClickListener {
 private static final String TAG = "huahua";
 private UserDBHelper mHelper;
 private EditText et_name;
 private EditText et_age;
 private EditText et_height;
 private EditText et_weight;
 private boolean Married = false;
 private String phone;
 private String password;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_shared_preferences);
 et_name = findViewById(R.id.et_name);
 et_age = findViewById(R.id.et_age);
 et_height = findViewById(R.id.et_height);
 et_weight = findViewById(R.id.et_weight);
 findViewById(R.id.btn_save).setOnClickListener(this);

 SharedPreferences sps = getSharedPreferences("Login",Context.MODE_PRIVATE);
 SharedPreferences.Editor editor = sps.edit();
 phone = sps.getString("phone","");
 password = sps.getString("password","");

 ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this, R.layout.item_dropdown, typeArray);
 typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
 Spinner sp_married = findViewById(R.id.sp_married);
 sp_married.setAdapter(typeAdapter);
 sp_married.setPrompt("请选择婚姻状况");
 sp_married.setSelection(0);
 sp_married.setOnItemSelectedListener(new TypeSelectedListener());

 }

 private String[] typeArray = {"未婚", "已婚"};

 class TypeSelectedListener implements AdapterView.OnItemSelectedListener {
 public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  Married = (arg2 == 0) ? false : true;
 }

 public void onNothingSelected(AdapterView<?> arg0) {
 }
 }

 @Override
 protected void onStart() {
 super.onStart();
 SQLiteDatabase mDB = getApplicationContext().openOrCreateDatabase("user.db", Context.MODE_PRIVATE, null);
 mHelper = UserDBHelper.getInstance(this, 1);
 mHelper.openWriteLink();
 }

 @Override
 protected void onStop() {
 super.onStop();
 mHelper.closeLink();
 }

 @Override
 public void onClick(View v) {
 if (v.getId() == R.id.btn_save) {
  String name = et_name.getText().toString();
  String age = et_age.getText().toString();
  String height = et_height.getText().toString();
  String weight = et_weight.getText().toString();
  if (name == null || name.length() <= 0) {
  showToast("请先填写姓名");
  return;
  }
  if (age == null || age.length() <= 0) {
  showToast("请先填写年龄");
  return;
  }
  if (height == null || height.length() <= 0) {
  showToast("请先填写身高");
  return;
  }
  if (weight == null || weight.length() <= 0) {
  showToast("请先填写体重");
  return;
  }

  UserInfo info = new UserInfo();

  info.name = name;
  info.age = Integer.parseInt(age);
  info.height = Long.parseLong(height);
  info.weight = Float.parseFloat(weight);
  info.married = Married;
  info.phone = phone;
  info.password = password;

  //info.update_time = DateUtil.getCurDateStr("yyyy-MM-dd HH:mm:ss");
  info.update_time = DateUtil.getNowDate(DateUtil.DatePattern.ALL_TIME);
  Log.d(TAG, "onClick: 手机号" + info.phone+info.password+info.name+info.update_time+info.married);
  mHelper.insert(info);
  Intent intent = new Intent(InfoWriteActivity.this, InfoReadActivity.class);
  startActivity(intent);
  showToast("数据已写入SQLite数据库");
 }
 }

 private void showToast(String desc) {
 Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
 }

 public static void startHome(Context mContext) {
 Intent intent = new Intent(mContext, InfoWriteActivity.class);
 mContext.startActivity(intent);
 }
}

InfoReadActivity

package com.example.helloworld;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class InfoWriteActivity extends AppCompatActivity implements View.OnClickListener {
 private static final String TAG = "huahua";
 private UserDBHelper mHelper;
 private EditText et_name;
 private EditText et_age;
 private EditText et_height;
 private EditText et_weight;
 private boolean Married = false;
 private String phone;
 private String password;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_shared_preferences);
 et_name = findViewById(R.id.et_name);
 et_age = findViewById(R.id.et_age);
 et_height = findViewById(R.id.et_height);
 et_weight = findViewById(R.id.et_weight);
 findViewById(R.id.btn_save).setOnClickListener(this);

 SharedPreferences sps = getSharedPreferences("Login",Context.MODE_PRIVATE);
 SharedPreferences.Editor editor = sps.edit();
 phone = sps.getString("phone","");
 password = sps.getString("password","");

 ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this, R.layout.item_dropdown, typeArray);
 typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
 Spinner sp_married = findViewById(R.id.sp_married);
 sp_married.setAdapter(typeAdapter);
 sp_married.setPrompt("请选择婚姻状况");
 sp_married.setSelection(0);
 sp_married.setOnItemSelectedListener(new TypeSelectedListener());
 }

 private String[] typeArray = {"未婚", "已婚"};

 class TypeSelectedListener implements AdapterView.OnItemSelectedListener {
 public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  Married = (arg2 == 0) ? false : true;
 }

 public void onNothingSelected(AdapterView<?> arg0) {
 }
 }

 @Override
 protected void onStart() {
 super.onStart();
 SQLiteDatabase mDB = getApplicationContext().openOrCreateDatabase("user.db", Context.MODE_PRIVATE, null);
 mHelper = UserDBHelper.getInstance(this, 1);
 mHelper.openWriteLink();
 }

 @Override
 protected void onStop() {
 super.onStop();
 mHelper.closeLink();
 }

 @Override
 public void onClick(View v) {
 if (v.getId() == R.id.btn_save) {
  String name = et_name.getText().toString();
  String age = et_age.getText().toString();
  String height = et_height.getText().toString();
  String weight = et_weight.getText().toString();
  if (name == null || name.length() <= 0) {
  showToast("请先填写姓名");
  return;
  }
  if (age == null || age.length() <= 0) {
  showToast("请先填写年龄");
  return;
  }
  if (height == null || height.length() <= 0) {
  showToast("请先填写身高");
  return;
  }
  if (weight == null || weight.length() <= 0) {
  showToast("请先填写体重");
  return;
  }

  UserInfo info = new UserInfo();

  info.name = name;
  info.age = Integer.parseInt(age);
  info.height = Long.parseLong(height);
  info.weight = Float.parseFloat(weight);
  info.married = Married;
  info.phone = phone;
  info.password = password;

  info.update_time = DateUtil.getNowDate(DateUtil.DatePattern.ALL_TIME);
  Log.d(TAG, "onClick: 手机号" + info.phone+info.password+info.name+info.update_time+info.married);
  mHelper.insert(info);
  Intent intent = new Intent(InfoWriteActivity.this, InfoReadActivity.class);
  startActivity(intent);
  showToast("数据已写入SQLite数据库");
 }
 }

 private void showToast(String desc) {
 Toast.makeText(this, desc, Toast.LENGTH_SHORT).show();
 }

 public static void startHome(Context mContext) {
 Intent intent = new Intent(mContext, InfoWriteActivity.class);
 mContext.startActivity(intent);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

时间: 2021-01-25

详解基于Android App 安全登录认证解决方案

近几年移动互联网的高速发展,智能手机的使用用户呈现爆炸性增长,手机终端上的App 种类繁多,大多数App 都需要与后台系统进行交互,交互的第一步需要进行登录认证,过于简单的认证方式可能被破解从而造成用户信息的泄露甚至威胁着用户的财产安全.为此基于Android 系统,对比现有几种常见的App 登录认证方式,并提出一种采用RSA 非对称加密和加入Token 时效机制的登录认证解决方案.在登录验证阶段采用RSA 非对称加密方式,App 端对服务器端返回的Token 信息加上时间戳,将处理后的Toke

Android实现简洁的APP登录界面

今天需求要做一个所有app都有的登录界面,正好巩固一下我们之前学的基础布局知识. 先来看下效果图 1.布局的xml文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent&qu

Android实现使用微信登录第三方APP的方法

本文实例讲述了Android实现使用微信登录第三方APP的方法.分享给大家供大家参考,具体如下: 使用微信登录APP,免去注册过程,现在已经有很多的类似应用了.集成该功能过程不复杂,但还是有一些地方需要注意的. 开始之前,需要做下面的准备工作. 1.到微信开放平台注册你的APP,并申请开通微信登录的权限.参考这里: https://open.weixin.qq.com// 2.下载Android SDK和签名查看工具,请参考: https://open.weixin.qq.com/cgi-bin

Android实现开机自动启动Service或app的方法

本文实例讲述了Android实现开机自动启动Service或app的方法.分享给大家供大家参考,具体如下: 第一步:首先创建一个广播接收者,重构其抽象方法 onReceive(Context context, Intent intent),在其中启动你想要启动的Service或app. import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; i

php微信支付之APP支付方法

本文实例讲述了微信开放平台移动应用集成微信支付功能.分享给大家供大家参考.具体分析如下: WechatAppPay文件代码如下: 复制代码 代码如下: <?php namespace common\services\WechatPay; class WechatAppPay extends WechatPayBase {     //package参数     public $package = [];     //异步通知参数     public $notify = [];     //推送

Android Kotlin仿微信头像裁剪图片的方法示例

0.前言 最近突发了很多事情,又跟康仔跳票了,无可奈何,不好意思了.最近生活上有很多感悟,一个男人的牛逼就在于平衡工作,学习和家庭,这个点很难把握,既要保证家庭和睦,又要保证自己价值的实现从而避免堕入平庸,每个人的状况都是不一样的,没有什么经验是可以照搬的,怎么说呢,不断摸索吧. 1.分析 整个效果是仿照微信来做的,效果如图所示: 整个效果就是从图库选取一张图片,并进行裁剪,从图库选取没什么好说的,就说说怎么做的裁剪控件吧,这个裁剪控件就是ClipImageView,可以看到它有一个阴影遮罩,一

使用Android开发接入第三方原生SDK实现微信登录

微信开放平台 : https://open.weixin.qq.com/ 一.准备工作 : 1. Android Studio环境下:在build.gradle文件中,添加如下依赖即可: dependencies { implementation'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:+' } 2. 在清单文件AndroidManifest.xml中加入权限: <uses-permission android:name="an

谈谈第三方App接入微信登录 解读

接入微信登录: 1.准备工作 1.在微信开放平台https://open.weixin.qq.com/注册成为开发者. 2.在"管理中心"中创建一个移动应用,需"应用名称.简介.及28*28和108*108的PNG图片各一张,且大小不超过300k",点击下一步,需"应用官网地址,应用签名及包名"等信息,然后即可提交审核. 说明: 应用签名:可在微信开发平台的资源中心>>资源下载>>中下载"签名生成工具"

iOS实现第三方微信登录方式实例解析(最新最全)

项目地址 : https://github.com/zhonggaorong/weixinLoginDemo 最新版本的微信登录实现步骤实现: 1.在进行微信OAuth2.0授权登录接入之前,在微信开放平台注册开发者帐号,并拥有一个已审核通过的移动应用,并获得相应的AppID和AppSecret,申请微信登录且通过审核后,可开始接入流程. 地址: 点击打开链接 2. 下载最新的SDK 地址: 点击打开链接 SDK内容如下: 结构解析: 从上到下依次说明: 1. 静态库,直接拖入工程. 2. re

详解Android微信登录与分享

Android 使用微信登录.分享功能 具体的文档详情微信官网上介绍(微信官网文档),本人直接按照项目部署步骤进行讲解: 第一步:申请你的AppID: 第二步:依赖 dependencies { compile 'com.tencent.mm.opensdk:wechat-sdk-android-with-mta:1.0.2' } 或者: dependencies { compile 'com.tencent.mm.opensdk:wechat-sdk-android-without-mta:1

Android 实现微信登录详解

前言 分享到微信朋友圈的功能早已经有了,但微信登录推出并不久,文档写的也并不是很清楚,这里记录分享一下.  正文 一.准备 注册.认证步骤自己来.微信开放平台:https://open.weixin.qq.com 二. 代码 2.1 唤起微信登录 参考官网文档:资源中心/移动应用开放/接入指南/Android接入指南 private IWXAPI mWeixinAPI; private void loginWithWeixin() { if (mWeixinAPI == null) { mWei

微信网页 第三方登录原理详解

微信开放平台和公众平台的区别 1.公众平台面向的时普通的用户,比如自媒体和媒体,企业官方微信公众账号运营人员使用,当然你所在的团队或者公司有实力去开发一些内容,也可以调用公众平台里面的接口,比如自定义菜单,自动回复,查询功能.目前大多数微信通过认证之后,都在做这个事情. 2.开放平台面向的开发者和第三方独立软件开发商.我觉得开发平台最大的开放就是微信登录.当年腾讯没有花大力气去做统一登录这个事情,导致目前各个网站都要弄一套登录机制.好在他们现在认清了局势.开发者或软件开发商,通过微信开放提供的平