该登录界面一共实现三个功能:
1.实现登录
2.实现注册
3.实现记住密码
AndroidManifest.xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"><applicationandroid:allowBackup="true"android:dataExtractionRules="@xml/data_extraction_rules"android:fullBackupContent="@xml/backup_rules"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.Smart_Campus"tools:targetApi="31"><activityandroid:name=".MainActivity"android:exported="true"android:launchMode="standard"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity><activity android:name=".loginActivity"/><activity android:name=".registeredActivity"/></application>
</manifest>
bean文件中的Account类:
package com.example.smart_campus.bean;public class Account {private int id;private String account;private String password;public Account(int id, String account, String password) {this.id = id;this.account = account;this.password = password;}public Account() {}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getAccount() {return account;}public void setAccount(String account) {this.account = account;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}
}
db文件夹中的AccountService:
package com.example.smart_campus.db;import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;import com.example.smart_campus.bean.Account;public class AccountService {private DataBaseHelper dataBaseHelper;public AccountService(Context context) {dataBaseHelper = new DataBaseHelper(context);}//登录public Boolean login(String account, String password){SQLiteDatabase sqLiteDatabase = dataBaseHelper.getReadableDatabase();String sql = "select * from Account where act = ? and pwd = ?";Cursor cursor = sqLiteDatabase.rawQuery(sql,new String[]{account,password});if (cursor.moveToFirst()) {cursor.close();return true;} else {return false;}}//注册public Boolean registered(Account account){SQLiteDatabase sqLiteDatabase = dataBaseHelper.getReadableDatabase();String sql = "insert into Account(act,pwd) values(?,?)";Object[] obj = {account.getAccount(),account.getPassword()};sqLiteDatabase.execSQL(sql,obj);return true;}
}
db文件夹中的DataBaseHelper:
package com.example.smart_campus.db;import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;import androidx.annotation.Nullable;public class DataBaseHelper extends SQLiteOpenHelper {public static final String DATABASE_NAME = "Smart_Campus.db";public static final int DATABASE_VERSION = 1;public static final String TABLE_NAME = "Account";public static final String COLUMN_ID = "id";public static final String COLUMN_Account = "act";public static final String COLUMN_Password = "pwd";public DataBaseHelper(@Nullable Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);}//创建数据库@Overridepublic void onCreate(SQLiteDatabase db) {String query = "CREATE TABLE " + TABLE_NAME +" (" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +COLUMN_Account + " VARCHAR(20), " +COLUMN_Password + " VARCHAR(20))";db.execSQL(query);}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}
MainActivity:
package com.example.smart_campus;import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;import com.example.smart_campus.db.AccountService;public class MainActivity extends AppCompatActivity {private SharedPreferences sharedPreferences;private SharedPreferences.Editor editor;private CheckBox rememberPass;@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Button button_login = findViewById(R.id.login);EditText account = findViewById(R.id.account);EditText password = findViewById(R.id.password);AccountService accountService = new AccountService(this);rememberPass = findViewById(R.id.reb_pwd);sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);boolean isRemember = sharedPreferences.getBoolean("remember_password", false);if (isRemember) {String reb_account = sharedPreferences.getString("account", "");String reb_password = sharedPreferences.getString("password", "");account.setText(reb_account);password.setText(reb_password);rememberPass.setChecked(true);}button_login.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {String act = account.getText().toString();String pwd = password.getText().toString();Boolean flag = accountService.login(act, pwd);if (flag) {editor = sharedPreferences.edit();if (rememberPass.isChecked()) {editor.putBoolean("remember_password", true);editor.putString("account", act);editor.putString("password", pwd);} else {editor.clear();}editor.commit();Intent intent = new Intent(MainActivity.this, loginActivity.class);startActivity(intent);} else {Toast.makeText(MainActivity.this, "账号或密码错误!", Toast.LENGTH_SHORT).show();}}});Button button_registered = findViewById(R.id.registered);button_registered.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent(MainActivity.this, registeredActivity.class);startActivity(intent);}});}
}
loginActivity
package com.example.smart_campus;import android.os.Bundle;import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;public class loginActivity extends AppCompatActivity {@Overridepublic void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.login);}
}
registeredActivity
package com.example.smart_campus;import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;import com.example.smart_campus.bean.Account;
import com.example.smart_campus.db.AccountService;public class registeredActivity extends AppCompatActivity {@Overridepublic void onCreate(@Nullable Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.registered);}public void myClick(View view){if (view.getId() == R.id.reg){AlertDialog.Builder builder = new AlertDialog.Builder(this);EditText new_account = findViewById(R.id.new_account);EditText new_pwd = findViewById(R.id.new_pwd);EditText re_pwd = findViewById(R.id.re_pwd);AccountService accountService = new AccountService(this);if (!new_account.getText().toString().isEmpty() && new_pwd.getText().toString().equals(re_pwd.getText().toString())){Account account = new Account();account.setAccount(new_account.getText().toString());account.setPassword(new_pwd.getText().toString());accountService.registered(account);builder.setTitle("提示");builder.setMessage("注册成功");builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {Intent intent = new Intent(registeredActivity.this, MainActivity.class);startActivity(intent);}});builder.setNegativeButton("取消",null);builder.show();} else {builder.setTitle("提示");builder.setMessage("注册失败!新账号为空或新密码与确认密码不同");builder.setNegativeButton("确认",null);builder.show();}}}
}
layout文件中的activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"android:orientation="vertical"><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="Hello Android"android:textStyle="bold"android:gravity="center"android:textSize="30dp"android:layout_marginTop="10dp"/><EditTextandroid:id="@+id/account"android:layout_width="200dp"android:layout_height="48dp"android:hint="请输入账号"android:layout_gravity="center"android:layout_marginTop="120dp"/><EditTextandroid:id="@+id/password"android:layout_width="200dp"android:layout_height="48dp"android:hint="请输入密码"android:inputType="textPassword"android:layout_gravity="center"android:layout_marginTop="10dp"/><CheckBoxandroid:id="@+id/reb_pwd"android:layout_width="100dp"android:layout_height="20dp"android:layout_marginLeft="100dp"android:text="记住密码"/><Buttonandroid:id="@+id/login"android:layout_width="200dp"android:layout_height="wrap_content"android:text="登录"android:layout_gravity="center"android:onClick="btnClick"android:layout_marginTop="10dp"/><Buttonandroid:id="@+id/registered"android:layout_width="200dp"android:layout_height="wrap_content"android:text="注册"android:layout_gravity="center"android:onClick="btnClick"/>
</LinearLayout>
layout中的login文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/login_text"android:layout_width="match_parent"android:layout_height="60dp"android:text="登录成功!"android:textSize="40dp"android:gravity="center"android:layout_gravity="center_vertical"/>
</LinearLayout>
layout文件中的registered
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:gravity="center_horizontal"><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:text="Hello Android"android:textStyle="bold"android:gravity="center"android:textSize="30dp"android:layout_marginTop="10dp"/><TextViewandroid:layout_width="match_parent"android:layout_height="50dp"android:layout_marginTop="30dp"android:text="注册界面"android:textStyle="bold"android:gravity="center"android:textSize="30dp"/><EditTextandroid:id="@+id/new_account"android:layout_width="200dp"android:layout_height="48dp"android:layout_marginTop="30dp"android:hint="输入新账号"android:layout_gravity="center"/><EditTextandroid:id="@+id/new_pwd"android:layout_width="200dp"android:layout_height="48dp"android:layout_marginTop="10dp"android:hint="输入新密码"android:layout_gravity="center"/><EditTextandroid:id="@+id/re_pwd"android:layout_width="200dp"android:layout_height="48dp"android:layout_marginTop="10dp"android:hint="确认新密码"android:layout_gravity="center"/><Buttonandroid:id="@+id/reg"android:layout_width="200dp"android:layout_height="48dp"android:layout_marginTop="30dp"android:text="确认注册"android:onClick="myClick"/></LinearLayout>