Android實(shí)現(xiàn)登錄注冊(cè)功能
本文實(shí)例為大家分享了Android實(shí)現(xiàn)登錄注冊(cè)功能的具體代碼,供大家參考,具體內(nèi)容如下
運(yùn)行環(huán)境 Android Studio
總體效果圖


一、 設(shè)計(jì)注冊(cè)頁面的布局
二、完成注冊(cè)功能
(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
{
? ? //創(chuàng)建一個(gè)List來緩存User信息
? ? List<User> userList = new ArrayList<>();
? ? //數(shù)據(jù)保存到這個(gè)文件
? ? File file;
? ? public UserManager(File file)
? ? {
? ? ? ? this.file = file;
? ? }
? ? //保存文件
? ? public void save() throws Exception
? ? {
? ? ? ? //每行存儲(chǔ)一個(gè)用戶的信息
? ? ? ? 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();
? ? }
? ? //注冊(cè)一個(gè)新用戶
? ? 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里面調(diào)用UserManager 實(shí)現(xiàn)注冊(cè)
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 中讀取數(shù)據(jù)
? ? ? ? }catch (Exception e){
? ? ? ? }
? ? ? ? //檢查用戶是否存在
? ? ? ? if(userManager.find(username) != null)
? ? ? ? {
? ? ? ? ? ? Toast.makeText(this, "用戶名已經(jīng)存在!", Toast.LENGTH_SHORT).show();
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? //添加用戶 保存文件
? ? ? ? ? ? userManager.add(new User(username,password));
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? userManager.save();
? ? ? ? ? ? }catch (Exception e){
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? ? ? Toast.makeText(this, "注冊(cè)成功!", Toast.LENGTH_SHORT).show();
? ? ? ? }
? ? }
}三、添加登錄頁面
(1)添加布局
(2)點(diǎn)擊注冊(cè)跳轉(zhuǎn)到登錄頁面
(3)點(diǎn)擊登錄能跳轉(zhuǎn)到主頁面
最后應(yīng)調(diào)用finish()關(guān)閉本界面,即從返回棧里銷毀本界面。原因是,當(dāng)用戶進(jìn)入主界面后,點(diǎn)返回時(shí)應(yīng)返回到Home主屏,而不應(yīng)該返回到登錄界面。
(可擴(kuò)展:保存登錄信息 自動(dòng)登錄)
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);
? ? }
? ? //點(diǎn)擊 '登錄' 按鈕
? ? 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();
? ? ? ? //從文件里加載所有用戶的數(shù)據(jù)
? ? ? ? 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, "密碼錯(cuò)誤!", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? //登錄成功 把用戶信息放在全局對(duì)象里
? ? ? ? //進(jìn)入主界面
? ? ? ? Intent intent = new Intent(UserLoginActivity.this,MainActivity.class);
? ? ? ? startActivity(intent);
? ? ? ? finish();
? ? }
? ? // 點(diǎn)擊 '注冊(cè)' 按鈕
? ? public void doRegister(View view)
? ? {
? ? ? ? Intent intent = new Intent(UserLoginActivity.this, RegisterActivity.class);
? ? ? ? startActivity(intent);
? ? }
}以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android用SharedPreferences實(shí)現(xiàn)登錄注冊(cè)注銷功能
- android實(shí)現(xiàn)注冊(cè)登錄程序
- Android實(shí)現(xiàn)登錄注冊(cè)頁面(下)
- Android基于Sqlite實(shí)現(xiàn)注冊(cè)和登錄功能
- Android實(shí)現(xiàn)注冊(cè)登錄界面的實(shí)例代碼
- Android設(shè)計(jì)登錄界面、找回密碼、注冊(cè)功能
- Android客戶端實(shí)現(xiàn)注冊(cè)、登錄詳解(1)
- Android登錄注冊(cè)功能 數(shù)據(jù)庫SQLite驗(yàn)證
- Android Studio連接SQLite數(shù)據(jù)庫的登錄注冊(cè)實(shí)現(xiàn)
- Android使用http實(shí)現(xiàn)注冊(cè)登錄功能
相關(guān)文章
Android中實(shí)現(xiàn)圓角圖片的幾種方法
本篇文章主要介紹了Android中實(shí)現(xiàn)圓角圖片的幾種方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-06-06
Android實(shí)現(xiàn)apk插件方式換膚的實(shí)例講解
在本篇文章里小編給大家整理的是關(guān)于Android實(shí)現(xiàn)apk插件方式換膚的實(shí)例代碼以及相關(guān)知識(shí)點(diǎn),有需要的朋友們學(xué)習(xí)下。2019-10-10
Android ListView中動(dòng)態(tài)添加RaidoButton的實(shí)例詳解
這篇文章主要介紹了Android ListView中動(dòng)態(tài)添加RaidoButton的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-08-08
Android開發(fā)實(shí)現(xiàn)模仿微信小窗口功能【Dialog對(duì)話框風(fēng)格窗口】
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)模仿微信小窗口功能,結(jié)合實(shí)例形式分析了Android實(shí)現(xiàn)微信風(fēng)格Dialog對(duì)話框窗口相關(guān)功能與布局操作技巧,需要的朋友可以參考下2019-03-03
基于RxJava2實(shí)現(xiàn)的簡單圖片爬蟲的方法
本篇文章主要介紹了基于RxJava2實(shí)現(xiàn)的簡單圖片爬蟲的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02
跨平臺(tái)移動(dòng)WEB應(yīng)用開發(fā)框架iMAG入門教程
這篇文章主要介紹了跨平臺(tái)移動(dòng)WEB應(yīng)用開發(fā)框架iMAG入門教程,iMAG最大的特點(diǎn)是生成各移動(dòng)平臺(tái)的原生代碼,需要的朋友可以參考下2014-07-07
Android開發(fā)常用標(biāo)簽小結(jié)
這篇文章主要介紹了Android開發(fā)常用標(biāo)簽,分析總結(jié)了Android開發(fā)中常見標(biāo)簽的使用技巧,需要的朋友可以參考下2015-05-05
關(guān)于AndroidStudio新建與編譯項(xiàng)目速度慢解決辦法
這篇文章主要介紹了關(guān)于AndroidStudio新建與編譯項(xiàng)目速度慢的解決辦法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10

