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


一、 設(shè)計注冊頁面的布局
二、完成注冊功能
(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)建一個List來緩存User信息
? ? List<User> userList = new ArrayList<>();
? ? //數(shù)據(jù)保存到這個文件
? ? 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里面調(diào)用UserManager 實現(xiàn)注冊
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, "注冊成功!", Toast.LENGTH_SHORT).show();
? ? ? ? }
? ? }
}三、添加登錄頁面
(1)添加布局
(2)點擊注冊跳轉(zhuǎn)到登錄頁面
(3)點擊登錄能跳轉(zhuǎn)到主頁面
最后應(yīng)調(diào)用finish()關(guān)閉本界面,即從返回棧里銷毀本界面。原因是,當(dāng)用戶進入主界面后,點返回時應(yīng)返回到Home主屏,而不應(yī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);
? ? }
? ? //點擊 '登錄' 按鈕
? ? 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, "密碼錯誤!", 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);
? ? }
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android用SharedPreferences實現(xiàn)登錄注冊注銷功能
- android實現(xiàn)注冊登錄程序
- Android實現(xiàn)登錄注冊頁面(下)
- Android基于Sqlite實現(xiàn)注冊和登錄功能
- Android實現(xiàn)注冊登錄界面的實例代碼
- Android設(shè)計登錄界面、找回密碼、注冊功能
- Android客戶端實現(xiàn)注冊、登錄詳解(1)
- Android登錄注冊功能 數(shù)據(jù)庫SQLite驗證
- Android Studio連接SQLite數(shù)據(jù)庫的登錄注冊實現(xiàn)
- Android使用http實現(xiàn)注冊登錄功能
相關(guān)文章
Android ListView中動態(tài)添加RaidoButton的實例詳解
這篇文章主要介紹了Android ListView中動態(tài)添加RaidoButton的實例詳解的相關(guān)資料,需要的朋友可以參考下2017-08-08
Android開發(fā)實現(xiàn)模仿微信小窗口功能【Dialog對話框風(fēng)格窗口】
這篇文章主要介紹了Android開發(fā)實現(xiàn)模仿微信小窗口功能,結(jié)合實例形式分析了Android實現(xiàn)微信風(fēng)格Dialog對話框窗口相關(guān)功能與布局操作技巧,需要的朋友可以參考下2019-03-03
跨平臺移動WEB應(yīng)用開發(fā)框架iMAG入門教程
這篇文章主要介紹了跨平臺移動WEB應(yīng)用開發(fā)框架iMAG入門教程,iMAG最大的特點是生成各移動平臺的原生代碼,需要的朋友可以參考下2014-07-07
Android開發(fā)常用標(biāo)簽小結(jié)
這篇文章主要介紹了Android開發(fā)常用標(biāo)簽,分析總結(jié)了Android開發(fā)中常見標(biāo)簽的使用技巧,需要的朋友可以參考下2015-05-05
關(guān)于AndroidStudio新建與編譯項目速度慢解決辦法
這篇文章主要介紹了關(guān)于AndroidStudio新建與編譯項目速度慢的解決辦法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-10-10

