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


一、 設(shè)計(jì)注冊(cè)頁(yè)面的布局
二、完成注冊(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來(lái)緩存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();
? ? ? ? }
? ? }
}三、添加登錄頁(yè)面
(1)添加布局
(2)點(diǎn)擊注冊(cè)跳轉(zhuǎn)到登錄頁(yè)面
(3)點(diǎn)擊登錄能跳轉(zhuǎn)到主頁(yè)面
最后應(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, "無(wú)此用戶!", 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);
? ? }
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android用SharedPreferences實(shí)現(xiàn)登錄注冊(cè)注銷功能
- android實(shí)現(xiàn)注冊(cè)登錄程序
- Android實(shí)現(xiàn)登錄注冊(cè)頁(yè)面(下)
- 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ù)庫(kù)SQLite驗(yàn)證
- Android Studio連接SQLite數(shù)據(jù)庫(kù)的登錄注冊(cè)實(shí)現(xiàn)
- Android使用http實(shí)現(xiàn)注冊(cè)登錄功能
相關(guān)文章
Android數(shù)據(jù)類型之間相互轉(zhuǎn)換系統(tǒng)介紹
一些初學(xué)Android的朋友可能會(huì)遇到JAVA的數(shù)據(jù)類型之間轉(zhuǎn)換的苦惱;本文將為有這類需求的朋友解決此類問(wèn)題2012-11-11
Kotlin作用域函數(shù)應(yīng)用詳細(xì)介紹
作用域函數(shù):是Kotlin標(biāo)準(zhǔn)庫(kù)中的內(nèi)聯(lián)函數(shù),作用在對(duì)象上時(shí),執(zhí)行給定的block代碼塊??梢栽赽lock代碼塊中通過(guò)it,this代表當(dāng)前對(duì)象,進(jìn)行代碼邏輯處理2022-08-08
詳解Android提交數(shù)據(jù)到服務(wù)器的兩種方式四種方法
本篇文章主要介紹了Android提交數(shù)據(jù)到服務(wù)器的兩種方式四種方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
微信或手機(jī)瀏覽器在線顯示office文件(已測(cè)試ios、android)
這篇文章主要介紹了微信或手機(jī)瀏覽器在線顯示office文件,已測(cè)試ios、android,感興趣的小伙伴們可以參考一下2016-06-06
Android中ListView如何分頁(yè)加載數(shù)據(jù)
這篇文章主要介紹了Android中ListView如何分頁(yè)加載數(shù)據(jù),本文就結(jié)合實(shí)例來(lái)演示一下使用ListView獲取數(shù)據(jù)的過(guò)程,需要的朋友可以參考下2015-12-12
Android控件之使用ListView實(shí)現(xiàn)時(shí)間軸效果
這篇文章主要介紹了Android基礎(chǔ)控件之使用ListView實(shí)現(xiàn)時(shí)間軸效果的相關(guān)資料,本文是以查看物流信息為例,給大家介紹了listview時(shí)間軸的實(shí)現(xiàn)代碼,需要的朋友可以參考下2016-11-11
Android自定義dialog 自下往上彈出的實(shí)例代碼
本文通過(guò)實(shí)例代碼給大家介紹了Android自定義dialog 自下往上彈出效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-08-08
Android?十六進(jìn)制狀態(tài)管理實(shí)例詳解
這篇文章主要為大家介紹了Android?十六進(jìn)制狀態(tài)管理實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09

