欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)登錄注冊功能

 更新時間:2022年04月23日 15:06:19   作者:大螃蟹蟹蟹  
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)登錄注冊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論