Android制作登錄頁面并且記住賬號密碼功能的實現(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="請輸入賬號"
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="請輸入密碼"
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>
二、代碼實現(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制作登錄頁面并且記住賬號密碼功能的實現(xiàn)代碼的文章就介紹到這了,更多相關(guān)android 登錄頁面記住密碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android中給fragment寫入?yún)?shù)的輕量開發(fā)包FragmentArgs簡介
這篇文章主要介紹了Android中給fragment寫入?yún)?shù)的輕量開發(fā)包FragmentArgs簡介,需要的朋友可以參考下2014-10-10
Android利用ScaleTransition實現(xiàn)吹氣球動畫
這篇文章主要為大家介紹了如何將利用ScaleTransition實現(xiàn)一個吹氣球的動畫,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下2022-04-04
Android studio 3.0 查看手機文件系統(tǒng)的方法(超簡單)
本文給大家分享Android studio更新到3.0版本之后,查看手機文件系統(tǒng)的方法,需要的朋友參考下吧2017-11-11
Android使用socket創(chuàng)建簡單TCP連接的方法
這篇文章主要介紹了Android使用socket創(chuàng)建簡單TCP連接的方法,結(jié)合實例形式詳細分析了Android使用socket創(chuàng)建TCP連接的具體步驟與實現(xiàn)技巧,需要的朋友可以參考下2016-04-04
Flutter之自定義Dialog實現(xiàn)版本更新彈窗功能的實現(xiàn)
這篇文章主要介紹了Flutter之自定義Dialog實現(xiàn)版本更新彈窗功能的實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07
Android中使用GridLayout網(wǎng)格布局來制作簡單的計算器App
這篇文章主要介紹了Android中使用GridLayout網(wǎng)格布局來制作簡單的計算器App的實例,GridLayout比表格布局TabelLayout更容易用來制作計算器這樣的多按鈕排列的界面,需要的朋友可以參考下2016-04-04

