Android制作登錄頁面并且記住賬號(hào)密碼功能的實(shí)現(xiàn)代碼

一、頁面搭建
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et_UserName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:ems="10"
android:hint="請(qǐng)輸入賬號(hào)"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/et_Password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:ems="10"
android:hint="請(qǐng)輸入密碼"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/et_UserName" />
<CheckBox
android:id="@+id/checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="記住密碼"
app:layout_constraintStart_toStartOf="@+id/et_Password"
app:layout_constraintTop_toBottomOf="@+id/et_Password" />
<Button
android:id="@+id/button"
android:onClick="Login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:text="安全登錄"
app:layout_constraintBottom_toBottomOf="@+id/checkBox"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/checkBox"
app:layout_constraintTop_toTopOf="@+id/checkBox" />
</android.support.constraint.ConstraintLayout>
二、代碼實(shí)現(xiàn)
package com.hiscene.test;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
public class MainActivity extends AppCompatActivity {
EditText et_userName;
EditText et_password;
CheckBox checkBox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_layout);
et_userName = findViewById(R.id.et_UserName);
et_password = findViewById(R.id.et_Password);
checkBox = findViewById(R.id.checkBox);
LoadInfo();
}
private void LoadInfo()
{
File file=new File("data/data/com.hiscene.test/usre.txt");
if (!file.exists()) return;
try {
FileReader reader = new FileReader(file);
BufferedReader br=new BufferedReader(reader);
String text=br.readLine();
String[] arr=text.split("#");
et_userName.setText(arr[0]);
et_password.setText(arr[1]);
checkBox.setChecked(true);
br.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public void Login(View view) {
String userName=et_userName.getText().toString().trim();
String password= et_password.getText().toString().trim();
if (TextUtils.isEmpty(userName)|| TextUtils.isEmpty(password))
{
Toast.makeText(MainActivity.this, "用戶名或密碼不能為空!", Toast.LENGTH_SHORT).show();
return;
}
if (checkBox.isChecked())
{
File file=new File("data/data/com.hiscene.test/usre.txt");
try {
OutputStream out=new FileOutputStream(file);
OutputStreamWriter osw=new OutputStreamWriter(out,"UTF-8");
BufferedWriter writer=new BufferedWriter(osw);
writer.write(userName+"#"+password);
writer.flush();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
總結(jié)
到此這篇關(guān)于Android制作登錄頁面并且記住賬號(hào)密碼功能的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)android 登錄頁面記住密碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
android中okhttp實(shí)現(xiàn)斷點(diǎn)上傳示例
本篇文章主要介紹了android中okhttp實(shí)現(xiàn)斷點(diǎn)上傳示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
Android中給fragment寫入?yún)?shù)的輕量開發(fā)包FragmentArgs簡(jiǎn)介
這篇文章主要介紹了Android中給fragment寫入?yún)?shù)的輕量開發(fā)包FragmentArgs簡(jiǎn)介,需要的朋友可以參考下2014-10-10
Android利用ScaleTransition實(shí)現(xiàn)吹氣球動(dòng)畫
這篇文章主要為大家介紹了如何將利用ScaleTransition實(shí)現(xiàn)一個(gè)吹氣球的動(dòng)畫,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2022-04-04
Android studio 3.0 查看手機(jī)文件系統(tǒng)的方法(超簡(jiǎn)單)
本文給大家分享Android studio更新到3.0版本之后,查看手機(jī)文件系統(tǒng)的方法,需要的朋友參考下吧2017-11-11
Android使用socket創(chuàng)建簡(jiǎn)單TCP連接的方法
這篇文章主要介紹了Android使用socket創(chuàng)建簡(jiǎn)單TCP連接的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android使用socket創(chuàng)建TCP連接的具體步驟與實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-04-04
Flutter之自定義Dialog實(shí)現(xiàn)版本更新彈窗功能的實(shí)現(xiàn)
這篇文章主要介紹了Flutter之自定義Dialog實(shí)現(xiàn)版本更新彈窗功能的實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Android中使用GridLayout網(wǎng)格布局來制作簡(jiǎn)單的計(jì)算器App
這篇文章主要介紹了Android中使用GridLayout網(wǎng)格布局來制作簡(jiǎn)單的計(jì)算器App的實(shí)例,GridLayout比表格布局TabelLayout更容易用來制作計(jì)算器這樣的多按鈕排列的界面,需要的朋友可以參考下2016-04-04
Android升級(jí)支持庫版本遇到的兩個(gè)問題詳解
安卓平臺(tái)其中一個(gè)很牛逼的地方在于它支持各種不同的設(shè)備。從你的平板電腦,到你的手機(jī),電視等,安卓無處不在。這篇文章主要給大家介紹了關(guān)于Android升級(jí)支持庫版本遇到的兩個(gè)問題,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來一起看看吧。2017-10-10

