用Android Studio 简单的实现登录注册
目录
activity_update_password.xml布局代码:
QQperate.java 对SQLite数据库进行增、改、查操作:
六、 styles.xml 和AndroidManifest.xml
一、登录界面
activity_login.xml 布局代码:
login.java 代码:
package com.example.datastorage;import androidx.annotation.Nullable;import androidx.appcompat.app.AppCompatActivity;import android.annotation.SuppressLint;import android.content.Intent;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.KeyEvent;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import java.util.Map;import java.util.Timer;import java.util.TimerTask;public class login extends AppCompatActivity implements View.OnClickListener { QQHelper qqHelper; private EditText login_z, login_m; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); qqHelper = new QQHelper(login.this);// login.this.deleteDatabase("qq.db"); init(); Outdate(); } private void init() { login_z = findViewById(R.id.login_z); login_m = findViewById(R.id.login_m); Button bt_register = findViewById(R.id.bt_register); Button bt_xpassword = findViewById(R.id.bt_xpassword); Button bt_login = findViewById(R.id.bt_login); bt_register.setOnClickListener(this); bt_xpassword.setOnClickListener(this); bt_login.setOnClickListener(this); } private void Outdate() { Map map = SaveQQ.getUserInfo(login.this); String account = map.get("account"); String password = map.get("password"); login_z.setText(account); login_m.setText(password); } @SuppressLint("NonConstantResourceId") @Override public void onClick(View view) { switch (view.getId()) { case R.id.bt_register: { Intent intent = new Intent(login.this , register.class); startActivity(intent); finish(); break; } case R.id.bt_xpassword: { Intent intent = new Intent(login.this , UpdatePassword.class); startActivity(intent); finish(); break; } case R.id.bt_login: { String account = login_z.getText().toString().trim(); String password = login_m.getText().toString(); Log.i("debug","登录按钮"); if(TextUtils.isEmpty(account) || TextUtils.isEmpty(password)) { Toast.makeText(this, "账号和密码不能为空", Toast.LENGTH_SHORT).show(); break; } int f = QQperate.query(login.this, account, password); if(f == 1) { Intent intent = new Intent(login.this , zhujianActivity.class); SaveQQ.SaveUserInfo(login.this, account, password); Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show(); startActivity(intent); finish(); } else { Toast.makeText(this, "账号或密码错误", Toast.LENGTH_SHORT).show(); } break; } } }}
二、注册界面
activity_register.xml布局代码:
register.java 代码:
package com.example.datastorage;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.KeyEvent;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.Toast;public class register extends AppCompatActivity { private EditText register_z, register_m, register_am, register_pnum; private ImageView back; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); init(); } private void init() { register_z = findViewById(R.id.register_z); register_pnum = findViewById(R.id.register_pnum); register_m = findViewById(R.id.register_m); register_am = findViewById(R.id.register_am); Button bt_rgcommit = findViewById(R.id.bt_rgcommit); back = findViewById(R.id.register_back); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { result(); } }); bt_rgcommit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String account = register_z.getText().toString(); String num = register_pnum.getText().toString().trim(); String password = register_m.getText().toString(); String apassword = register_am.getText().toString(); if(TextUtils.isEmpty(account)) { Toast.makeText(register.this, "账号不能为空", Toast.LENGTH_SHORT).show(); return; } if(TextUtils.isEmpty(num)) { Toast.makeText(register.this, "号码不能为空", Toast.LENGTH_SHORT).show(); return; } if(TextUtils.isEmpty(password)) { Toast.makeText(register.this, "密码不能为空", Toast.LENGTH_SHORT).show(); return; } if(TextUtils.isEmpty(apassword)) { Toast.makeText(register.this, "确认密码不能为空", Toast.LENGTH_SHORT).show(); return; } if(!password.equals(apassword)) { Toast.makeText(register.this, "密码和确认密码不相同", Toast.LENGTH_SHORT).show(); return; } int f = QQperate.insert(register.this, account, num, password); if (f == 1) { SaveQQ.SaveUserInfo(register.this , account, password); Toast.makeText(register.this, "注册成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(register.this, "注册失败,账号或者号码已存在", Toast.LENGTH_SHORT).show(); } } }); } private void result() { Intent intent = new Intent(register.this , login.class); startActivity(intent); finish(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { result(); } return false; }}
三、修改密码(忘记密码 ?)
activity_update_password.xml布局代码:
UpdatePassword.java 代码:
package com.example.datastorage;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.text.TextUtils;import android.view.KeyEvent;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.ImageView;import android.widget.Toast;import java.util.Map;public class UpdatePassword extends AppCompatActivity { private EditText up_pnum, up_m, up_am; private ImageView back; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_update_password); init(); } private void init() { up_pnum = findViewById(R.id.up_pnum); up_m = findViewById(R.id.up_m); up_am = findViewById(R.id.up_am); Button bt_upcommit = findViewById(R.id.bt_upcommit); back = findViewById(R.id.password_back); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { result(); } }); bt_upcommit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String num = up_pnum.getText().toString().trim(); String password = up_m.getText().toString(); String apassword = up_am.getText().toString(); if (TextUtils.isEmpty(num)) { Toast.makeText(UpdatePassword.this, "号码不能为空", Toast.LENGTH_SHORT).show(); return; } if (TextUtils.isEmpty(password)) { Toast.makeText(UpdatePassword.this, "新密码不能为空", Toast.LENGTH_SHORT).show(); return; } if (TextUtils.isEmpty(apassword)) { Toast.makeText(UpdatePassword.this, "确认密码不能为空", Toast.LENGTH_SHORT).show(); return; } if (!password.equals(apassword)) { Toast.makeText(UpdatePassword.this, "确认密码不能为空", Toast.LENGTH_SHORT).show(); return; } int f = QQperate.update(UpdatePassword.this , num, password); if(f == 1) { Map map = QQperate.queryUp(UpdatePassword.this , num); SaveQQ.SaveUserInfo(UpdatePassword.this , map.get("account"), map.get("password")); Toast.makeText(UpdatePassword.this, "修改成功", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(UpdatePassword.this, "修改失败", Toast.LENGTH_SHORT).show(); } } }); } private void result() { Intent intent = new Intent(UpdatePassword.this , login.class); startActivity(intent); finish(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { result(); } return false; }}
四、SQLite 与 SharedPreferences 操作
SQlite数据库存储主要用来保存用户信息,SharedPreferences存储主要用来实现记录账号与密码的功能,方便下次登录。
QQHelper.java 创建SQLite数据库:
package com.example.datastorage;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import androidx.annotation.Nullable;public class QQHelper extends SQLiteOpenHelper { public QQHelper(@Nullable Context context) { super(context, "qq.db", null, 1); } @Override public void onCreate(SQLiteDatabase sqLiteDatabase) { String sql = "create table user(_id Integer primary key AUTOINCREMENT, account varchar(20) , num varchar(20), password varchar(20))"; sqLiteDatabase.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { }}
QQperate.java 对SQLite数据库进行增、改、查操作:
package com.example.datastorage;import android.annotation.SuppressLint;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.util.Log;import java.util.HashMap;import java.util.Map;public class QQperate { //添加用户 public static int insert(Context context, String account, String num, String password) { QQHelper qqHelper = new QQHelper(context); SQLiteDatabase db = qqHelper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("account",account); values.put("num",num); values.put("password",password); String sql = "select * from user where account=? or num=?"; Cursor cursor = db.rawQuery(sql, new String[]{account, num}); int f = 0; if(cursor.getCount() == 0) { db.insert("user",null,values); f = 1; } db.close(); cursor.close(); return f; } //修改密码 public static int update(Context context, String num, String password) { QQHelper qqHelper = new QQHelper(context); SQLiteDatabase db = qqHelper.getWritableDatabase(); String sql = "update user set password=? where num=?"; Cursor cursor = db.query("user",null,"num=?",new String[]{num},null,null,null); int f = 0; if(cursor.getCount() != 0) { db.execSQL(sql,new String[]{password, num}); f = 1; } db.close(); cursor.close(); return f; } //查询用户 public static int query(Context context, String account, String password) { QQHelper qqHelper = new QQHelper(context); SQLiteDatabase db = qqHelper.getWritableDatabase(); String sql = "select * from user where account=? and password=?"; Cursor cursor = db.rawQuery(sql,new String[]{account,password}); int f = 0; if(cursor.getCount() != 0) f = 1; db.close(); cursor.close(); return f; } //通过注册号码查询账号与密码 public static Map queryUp(Context context, String num) { QQHelper qqHelper = new QQHelper(context); SQLiteDatabase db = qqHelper.getWritableDatabase(); String sql = "select * from user where num=?"; Cursor cursor = db.rawQuery(sql,new String[]{num}); Map map = new HashMap<>(); if(cursor.moveToNext()) { @SuppressLint("Range") String account = cursor.getString(cursor.getColumnIndex("account")); @SuppressLint("Range") String password = cursor.getString(cursor.getColumnIndex("password")); map.put("account",account); map.put("password",password); } cursor.close(); db.close(); return map; }}
SaveQQ.java保存QQ账号与密码:
package com.example.datastorage;import android.content.Context;import android.content.SharedPreferences;import java.util.HashMap;import java.util.Map;public class SaveQQ { public static void SaveUserInfo(Context context, String account, String password) { SharedPreferences sharedPreferences = context.getSharedPreferences("QQuser",Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("account",account); editor.putString("password",password); editor.apply(); } public static Map getUserInfo(Context context) { SharedPreferences sharedPreferences = context.getSharedPreferences("QQuser",Context.MODE_PRIVATE); String account = sharedPreferences.getString("account",null); String password = sharedPreferences.getString("password",null); Map map = new HashMap<>(); map.put("account",account); map.put("password",password); return map; }}
五、登录成功界面
activity_zhujian.xml 代码:
zhujianActivity.java 代码:
package com.example.datastorage;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;public class zhujianActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zhujian); }}
六、 styles.xml 和AndroidManifest.xml
在创建Activity之后,需要对创建的Activity进行配置,在AndroidManifest.xml中可以看到Activity的配置,其中exported根据需要设置,启动模式可以选择默认,stles.xml里面设置的是竖线。(AndroidManifest.xml 中有不是登录注册的Activity配置,不要混淆)
styles.xml 代码:
AndroidManifest.xml 代码:
来源地址:https://blog.csdn.net/qq_51936803/article/details/130072031