今天,我要分享给大家的是Android中常见的一个的登录注册的案例,我这里写的是简易版,如果大家有更精彩的拓展,可以自行发挥哦!
运行过程相信大家都已经心知肚明了,所以我在这里就直接发布代码了,其中有不理解的地方大家可以自行百度,也可以互相学习讨论。如有错误,麻烦大家在评论区留言,谢谢。
AndroidManifest.xml文件:
<applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.WeChat"><activity android:name=".LoginActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".RegisterActivity" /><activity android:name=".MainActivity"/></application>
布局代码:
styles.xml
<style name="hLine"><item name="android:layout_width">match_parent</item><item name="android:layout_height">1dp</item><item name="android:background">@color/black</item></style><style name="tvTwo"><item name="android:layout_width">wrap_content</item><item name="android:layout_height">wrap_content</item><item name="android:layout_marginLeft">20dp</item><item name="android:textColor">@color/black</item><item name="android:textSize">15sp</item></style><style name="etOne"><item name="android:layout_width">match_parent</item><item name="android:layout_height">wrap_content</item><item name="android:layout_marginLeft">30dp</item><item name="android:background">@null</item><item name="android:textColor">@color/black</item></style>
activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="用户登录/LOGIN"android:textSize="30sp"android:textStyle="bold"android:textColor="@color/black"android:layout_marginTop="50dp"/><Viewstyle="@style/hLine"android:layout_marginTop="40dp"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="15dp"><TextViewstyle="@style/tvTwo"android:text="性 名:" /><EditTextandroid:id="@+id/lg_name"style="@style/etOne" /></LinearLayout><View style="@style/hLine" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="15dp"><TextViewstyle="@style/tvTwo"android:text="密 码:" /><EditTextandroid:id="@+id/lg_psw"style="@style/etOne"android:inputType="textPassword"/></LinearLayout><View style="@style/hLine" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginTop="50dp"><Buttonandroid:id="@+id/btn_login"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="25dp"android:layout_marginLeft="50dp"android:text="登 录"android:background="@color/gray"/><Buttonandroid:id="@+id/btn_register"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="25dp"android:layout_marginLeft="120dp"android:text="注 册"android:background="@color/gray"/></LinearLayout>
</LinearLayout>
activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:text="用户注册"android:textColor="@color/black"android:textSize="40sp"/><View style="@style/hLine" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="15dp"><TextViewstyle="@style/tvTwo"android:text="姓名:" /><EditTextandroid:id="@+id/rg_name"style="@style/etOne" /></LinearLayout><View style="@style/hLine" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:padding="15dp"><TextViewstyle="@style/tvTwo"android:text="密码:" /><EditTextandroid:id="@+id/rg_psw"style="@style/etOne"android:inputType="textPassword" /></LinearLayout><View style="@style/hLine" /><Buttonandroid:id="@+id/btn_submit"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="120dp"android:gravity="center"android:text="提交"android:background="@color/gray"android:textSize="18sp" />
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:orientation="horizontal"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="30dp"android:text="祝你,登录成功,未来可期!"android:textColor="#E53935"android:textSize="30sp"/>
</LinearLayout>
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;@Overrideprotected 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;@Overrideprotected 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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}
}
像登录注册此类问题的案例,我们要特别注意逻辑性,涉及到数据量比较多、杂的话,我们可以用
笔将它们记录在本子上,世上无难事,只怕有心人,让我们一起加油吧!!!