Android實現(xiàn)登錄注冊功能
本文實例為大家分享了Android實現(xiàn)登錄注冊功能的具體代碼,供大家參考,具體內(nèi)容如下
運(yùn)行環(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)用戶進(jìn)入主界面后,點返回時應(yīng)返回到Home主屏,而不應(yīng)該返回到登錄界面。
(可擴(kuò)展:保存登錄信息 自動登錄)
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; ? ? ? ? } ? ? ? ? //登錄成功 把用戶信息放在全局對象里 ? ? ? ? //進(jìn)入主界面 ? ? ? ? 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數(shù)據(jù)類型之間相互轉(zhuǎn)換系統(tǒng)介紹
一些初學(xué)Android的朋友可能會遇到JAVA的數(shù)據(jù)類型之間轉(zhuǎn)換的苦惱;本文將為有這類需求的朋友解決此類問題2012-11-11Kotlin作用域函數(shù)應(yīng)用詳細(xì)介紹
作用域函數(shù):是Kotlin標(biāo)準(zhǔn)庫中的內(nèi)聯(lián)函數(shù),作用在對象上時,執(zhí)行給定的block代碼塊。可以在block代碼塊中通過it,this代表當(dāng)前對象,進(jìn)行代碼邏輯處理2022-08-08詳解Android提交數(shù)據(jù)到服務(wù)器的兩種方式四種方法
本篇文章主要介紹了Android提交數(shù)據(jù)到服務(wù)器的兩種方式四種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2016-11-11微信或手機(jī)瀏覽器在線顯示office文件(已測試ios、android)
這篇文章主要介紹了微信或手機(jī)瀏覽器在線顯示office文件,已測試ios、android,感興趣的小伙伴們可以參考一下2016-06-06Android中ListView如何分頁加載數(shù)據(jù)
這篇文章主要介紹了Android中ListView如何分頁加載數(shù)據(jù),本文就結(jié)合實例來演示一下使用ListView獲取數(shù)據(jù)的過程,需要的朋友可以參考下2015-12-12Android控件之使用ListView實現(xiàn)時間軸效果
這篇文章主要介紹了Android基礎(chǔ)控件之使用ListView實現(xiàn)時間軸效果的相關(guān)資料,本文是以查看物流信息為例,給大家介紹了listview時間軸的實現(xiàn)代碼,需要的朋友可以參考下2016-11-11Android?十六進(jìn)制狀態(tài)管理實例詳解
這篇文章主要為大家介紹了Android?十六進(jìn)制狀態(tài)管理實例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09