網站首頁 編程語言 正文
本文實例為大家分享了Android實現登錄注冊功能的具體代碼,供大家參考,具體內容如下
運行環境 Android Studio
總體效果圖
一、 設計注冊頁面的布局
二、完成注冊功能
(1) 添加User類
(2)添加 UserManager類 管理用戶信息
package com.example.videoplayer;
import android.hardware.usb.UsbRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
?* Created by 大頭 on 2020/5/28.
?*/
public class UserManager
{
? ? //創建一個List來緩存User信息
? ? List<User> userList = new ArrayList<>();
? ? //數據保存到這個文件
? ? File file;
? ? public UserManager(File file)
? ? {
? ? ? ? this.file = file;
? ? }
? ? //保存文件
? ? public void save() throws Exception
? ? {
? ? ? ? //每行存儲一個用戶的信息
? ? ? ? FileOutputStream fileOutputStream = new FileOutputStream(file);
? ? ? ? for (User u : userList)
? ? ? ? {
? ? ? ? ? ? String line = u.username + "," + u.password + "\n";
? ? ? ? ? ? fileOutputStream.write(line.getBytes("UTF-8"));
? ? ? ? }
? ? ? ? fileOutputStream.close();
? ? }
? ? //從文件加載
? ? public void load() throws Exception
? ? {
? ? ? ? InputStreamReader in = new InputStreamReader(new FileInputStream(file));
? ? ? ? BufferedReader reader = new BufferedReader(in);
? ? ? ? userList.clear();//清空鏈表
? ? ? ? while (true)
? ? ? ? {
? ? ? ? ? ? String line = reader.readLine();
? ? ? ? ? ? if (line == null)
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? String[] cols = line.split(",");
? ? ? ? ? ? if (cols.length<2) continue;
? ? ? ? ? ? User user = new User();
? ? ? ? ? ? user.username = cols[0].trim();
? ? ? ? ? ? user.password = cols[1].trim();
? ? ? ? ? ? userList.add( user );
? ? ? ? }
? ? ? ? reader.close();
? ? }
? ? //注冊一個新用戶
? ? public void add(User u)
? ? {
? ? ? ? userList.add(u);
? ? }
? ? // 按名稱查找
? ? public User find(String username)
? ? {
? ? ? ? for (User u : userList)
? ? ? ? {
? ? ? ? ? ? if(u.username.equals(username))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? return u;
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? return null;
? ? }
}
(3)在RegisterActivity里面調用UserManager 實現注冊
package com.example.videoplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
public class RegisterActivity extends AppCompatActivity
{
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState)
? ? {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_register);
? ? }
? ? public void doRegister(View view)
? ? {
? ? ? ? //獲取用戶輸入
? ? ? ? String username = ((EditText)findViewById(R.id.id_username)).getText().toString();
? ? ? ? String password = ((EditText)findViewById(R.id.id_password)).getText().toString();
? ? ? ? String password2 = ((EditText)findViewById(R.id.id_password2)).getText().toString();
? ? ? ? if(!password.equals(password2))
? ? ? ? {
? ? ? ? ? ? Toast.makeText(this,"兩次密碼不一致",Toast.LENGTH_SHORT).show();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? //保存用戶信息
? ? ? ? File file = new File(getExternalFilesDir(""),"users.txt");
? ? ? ? UserManager userManager = new UserManager(file);
? ? ? ? try {
? ? ? ? ? ? userManager.load();//從users.txt 中讀取數據
? ? ? ? }catch (Exception e){
? ? ? ? }
? ? ? ? //檢查用戶是否存在
? ? ? ? if(userManager.find(username) != null)
? ? ? ? {
? ? ? ? ? ? Toast.makeText(this, "用戶名已經存在!", Toast.LENGTH_SHORT).show();
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? //添加用戶 保存文件
? ? ? ? ? ? userManager.add(new User(username,password));
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? userManager.save();
? ? ? ? ? ? }catch (Exception e){
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? ? ? Toast.makeText(this, "注冊成功!", Toast.LENGTH_SHORT).show();
? ? ? ? }
? ? }
}
三、添加登錄頁面
(1)添加布局
(2)點擊注冊跳轉到登錄頁面
(3)點擊登錄能跳轉到主頁面
最后應調用finish()關閉本界面,即從返回棧里銷毀本界面。原因是,當用戶進入主界面后,點返回時應返回到Home主屏,而不應該返回到登錄界面。
(可擴展:保存登錄信息 自動登錄)
package com.example.videoplayer;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.io.File;
public class UserLoginActivity extends AppCompatActivity {
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_user_login);
? ? }
? ? //點擊 '登錄' 按鈕
? ? public void doLogin(View view)
? ? {
? ? ? ? // 取得用戶界面輸入
? ? ? ? String username = ((EditText)findViewById(R.id.id_username)).getText().toString();
? ? ? ? String password = ((EditText)findViewById(R.id.id_password)).getText().toString();
? ? ? ? //從文件里加載所有用戶的數據
? ? ? ? File file = new File(getExternalFilesDir(""),"users.txt");
? ? ? ? UserManager userManager = new UserManager(file);
? ? ? ? try {
? ? ? ? ? ? userManager.load();
? ? ? ? }catch (Exception e){}
? ? ? ? //從用戶列表里查找用戶
? ? ? ? User user = userManager.find(username);
? ? ? ? if (user == null)
? ? ? ? {
? ? ? ? ? ? Toast.makeText(this, "無此用戶!", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? //比較密碼是否匹配
? ? ? ? if (!user.password.equals(password))
? ? ? ? {
? ? ? ? ? ? Toast.makeText(this, "密碼錯誤!", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? //登錄成功 把用戶信息放在全局對象里
? ? ? ? //進入主界面
? ? ? ? Intent intent = new Intent(UserLoginActivity.this,MainActivity.class);
? ? ? ? startActivity(intent);
? ? ? ? finish();
? ? }
? ? // 點擊 '注冊' 按鈕
? ? public void doRegister(View view)
? ? {
? ? ? ? Intent intent = new Intent(UserLoginActivity.this, RegisterActivity.class);
? ? ? ? startActivity(intent);
? ? }
}
原文鏈接:https://blog.csdn.net/tttttt521/article/details/106412240
相關推薦
- 2022-05-17 Springboot+Maven做啟動類與業務模塊分離的架構模式
- 2022-07-18 Ubuntu 18.04 共享文件夾 與其他系統互傳文件
- 2023-11-26 (有效解決)Android Studio 運行項目時報 Package install error:
- 2023-03-23 Android進階NestedScroll嵌套滑動機制實現吸頂效果詳解_Android
- 2022-01-21 什么是前端開發?什么是后端開發?
- 2022-05-23 Android實現手機聯系人分欄效果_Android
- 2022-04-15 python3?cmp實現方式_python
- 2022-07-26 Python程序元素分析和注意事項
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支