Android实现简单的登录界面

news/2024/11/29 18:34:37/

该登录界面一共实现三个功能:

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>

http://www.ppmy.cn/news/30318.html

相关文章

网络编程学习笔记(1)--TCP/IP协议基础

目录 全称 定义 分层结构 端到端的数据传输 ​编辑 举个栗子——浏览网页 应用层 传输层 TCP UDP 网络层 IP协议 特点 IP地址 网络掩码 ARP协议 PARP协议 ICMP协议 数据链路层 名词解释 数据段 数据报 数据包 数据帧 比特流 全称 Transmission Contro…

brew安装问题

最近使用mac安装了Python和PyCharm&#xff0c;使用python中的绘制图像的turtle库后&#xff0c;执行报错&#xff1a; import _tkinter # If this fails your Python may not be configured for Tk ModuleNotFoundError: No module named _tkinter 查询后需在mac 命令行执行&…

【UML】软件需求说明书

目录&#x1f981; 故事的开端一. &#x1f981; 引言1.1编写目的1.2背景1.3定义1.4参考资料二. &#x1f981; 任务概述2.1目标2.2用户的特点2.3假定和约束三. &#x1f981; 需求规定3.1 功能性需求3.1.1系统用例图3.1.2用户登录用例3.1.3学员注册用例3.1.4 学员修改个人信息…

C++——类型转换

目录 C语言中的类型转换 C强制类型转换 static_cast reinterpret_cast const_cast dynamic_cast 延伸问题 RTTI&#xff08;了解&#xff09; C语言中的类型转换 在C语言中&#xff0c;如果赋值运算符左右两侧类型不同&#xff0c;或者形参与实参类型不匹配&#xff0c;或…

Maven专题总结—详细版

第一章 为什么使用Maven 获取jar包 使用Maven之前&#xff0c;自行在网络中下载jar包&#xff0c;效率较低。如【谷歌、百度、CSDN…】使用Maven之后&#xff0c;统一在一个地址下载资源jar包【阿里云镜像服务器等…】 添加jar包 使用Maven之前&#xff0c;将jar复制到项目工程…

【微信小程序】-- 页面导航 -- 声明式导航(二十二)

&#x1f48c; 所属专栏&#xff1a;【微信小程序开发教程】 &#x1f600; 作  者&#xff1a;我是夜阑的狗&#x1f436; &#x1f680; 个人简介&#xff1a;一个正在努力学技术的CV工程师&#xff0c;专注基础和实战分享 &#xff0c;欢迎咨询&#xff01; &…

2023年再不会 IOC 源码,就要被淘汰了

&#x1f44f;作者简介&#xff1a;大家好&#xff0c;我是爱敲代码的小黄&#xff0c;独角兽企业的Java开发工程师&#xff0c;CSDN博客专家&#xff0c;阿里云专家博主&#x1f4d5;系列专栏&#xff1a;Java设计模式、数据结构和算法、Kafka从入门到成神、Kafka从成神到升仙…

【计算机组成原理 - 第一章】计算机系统概论(完结)

本章参考王道考研相关课程&#xff1a; 【2021版】1.2.1_计算机硬件的基本组成_哔哩哔哩_bilibili 【2021版】1.2.2_认识各个硬件部件_哔哩哔哩_bilibili 【2021版】1.2.3_计算机系统的层次结构_哔哩哔哩_bilibili 【2021版】1.3_计算机的性能指标_哔哩哔哩_bilibili 目录 一、…