今天,我要分享给大家的是Android中常见的一个的登录注册的案例,我这里写的是简易版,如果大家有更精彩的拓展,可以自行发挥哦!
运行过程相信大家都已经心知肚明了,所以我在这里就直接发布代码了,其中有不理解的地方大家可以自行百度,也可以互相学习讨论。如有错误,麻烦大家在评论区留言,谢谢。
AndroidManifest.xml文件:
布局代码:
styles.xml
activity_login.xml
activity_register.xml
activity_main.xml
Java代码:
LoginActivity.java
package com.example.wechat;import androidx.appcompat.app.AppCompatActivity;import android.annotation.SuppressLint;import android.content.Intent;import android.os.Bundle;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import android.content.SharedPreferences;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;public class LoginActivity extends AppCompatActivity { EditText lg_name,lg_psw; Button btn_login; Button btn_register; SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); lg_name=findViewById(R.id.lg_name); lg_psw=findViewById(R.id.lg_psw); btn_login=findViewById(R.id.btn_login); btn_register=findViewById(R.id.btn_register); //登录注册部分数据库 db=SQLiteDatabase.openOrCreateDatabase(getCacheDir()+"/note",null); try { db.execSQL("create table user(username varchar(100),password varchar(100))"); } catch (Exception e){ e.printStackTrace(); } SharedPreferences sharedPreferences=getSharedPreferences("user",0); lg_name.setText(sharedPreferences.getString("lg_name","")); lg_psw.setText(sharedPreferences.getString("lg_psw","")); btn_login.setOnClickListener(view -> { if (lg_name.getText().toString().equals("") || lg_psw.getText().toString().equals("")){ Toast.makeText(LoginActivity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show(); return; } @SuppressLint("Recycle") Cursor cursor=db.rawQuery("select * from user where username='"+lg_name.getText().toString()+"'",null); if (cursor.moveToNext()){ if (cursor.getString(1).equals(lg_psw.getText().toString())){ SharedPreferences.Editor editor=getSharedPreferences("lg_name",0).edit(); Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show(); startActivity(new Intent(LoginActivity.this,MainActivity.class)); editor.apply(); }else { Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show(); } }else { Toast.makeText(LoginActivity.this, "账号不存在", Toast.LENGTH_SHORT).show(); } }); btn_register.setOnClickListener(view -> startActivity(new Intent(LoginActivity.this,RegisterActivity.class))); }}
RegisterActivity.java
package com.example.wechat;import android.annotation.SuppressLint;import android.content.ContentValues;import android.content.Intent;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;public class RegisterActivity extends AppCompatActivity { EditText rg_name,rg_psw; Button btn_submit; SQLiteDatabase db; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); db=SQLiteDatabase.openOrCreateDatabase(getCacheDir()+"/note",null); rg_psw=findViewById(R.id.rg_psw); rg_name=findViewById(R.id.rg_name); btn_submit=findViewById(R.id.btn_submit); btn_submit.setOnClickListener(view -> { if (rg_name.getText().toString().equals("") || rg_psw.getText().toString().equals("")){ Toast.makeText(RegisterActivity.this, "账号或密码不能为空", Toast.LENGTH_SHORT).show(); return; } @SuppressLint("Recycle") Cursor cursor=db.rawQuery("select * from user where username='"+rg_name.getText().toString()+"'",null); if (cursor.moveToNext()){ Toast.makeText(RegisterActivity.this, "账号已存在", Toast.LENGTH_SHORT).show(); }else { ContentValues contentValues = new ContentValues(); contentValues.put("username", rg_name.getText().toString()); contentValues.put("password", rg_psw.getText().toString()); db.insert("user", null, contentValues); Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show(); startActivity(new Intent(RegisterActivity.this, LoginActivity.class)); } }); }}
MainActivity.java
package com.example.wechat;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }}
像登录注册此类问题的案例,我们要特别注意逻辑性,涉及到数据量比较多、杂的话,我们可以用
笔将它们记录在本子上,世上无难事,只怕有心人,让我们一起加油吧!!!