登录页面实现,数据存储将数据保存到内部文件,到再次加载页面,再从内部文件中获取
创建安卓项目
编写activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainActivity"
android:padding="10dp"
android:orientation="vertical">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_gravity="center_horizontal"
android:layout_margin="30dp"
android:src="@drawable/head"></ImageView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal"
android:background="@android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="账号"
android:textColor="#000"
android:textSize="20sp"
/>
<EditText
android:id="@+id/et_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:padding="10dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal"
android:background="@android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="密码"
android:textColor="#000"
android:textSize="20sp"></TextView>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:padding="10dp" />
</LinearLayout>
<Button
android:id="@+id/btn_Login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#3C8DC4"
android:text="登录"
android:textColor="@android:color/white"
android:textSize="20sp"
/>
</LinearLayout>
编写MainActivity:
package com.example.a2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_account;
private EditText et_password;
private Button btn_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_account =(EditText)findViewById(R.id.et_account);
et_password =(EditText)findViewById(R.id.et_password);
btn_login =(Button) findViewById(R.id.btn_Login);
btn_login.setOnClickListener(this);//创建监听事件
Map<String,String> map=getUserInfo();
et_account.setText(map.get("账号"));
et_password.setText(map.get("密码"));
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id .btn_Login:
String account=et_account.getText().toString().trim();
String password=et_password.getText().toString().trim();
if (TextUtils.isEmpty(account)){
Toast.makeText(this,"账号为空",Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)){
Toast.makeText(this,"密码为空",Toast.LENGTH_SHORT).show();
return;
}else{
Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
}
//保存文件
boolean issucess=SaveQQ(account,password);
if(issucess==true){
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
}
break;
}
}
//保存文件
private boolean SaveQQ(String account, String password) {
FileOutputStream fos=null;
try{
fos=openFileOutput("data.txt",MODE_PRIVATE);
fos.write((account+"="+password).getBytes());
fos.close();
return true;
}catch(IOException e){
e.printStackTrace();
return false;
}
}
//读取文件
private Map<String,String> getUserInfo(){
FileInputStream fis = null;
try {
fis = openFileInput("data.txt");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
String info = new String(buffer);
String[] infos = info.split("=");
Map<String, String> map = new HashMap<>();
map.put("账号", infos[0]);
map.put("密码", infos[1]);
fis.close();
return map;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
}
点击返回:
另种方式:SharedPreferences存储 ,与上面只有MainActivity里内容不同:
package com.example.wenjianbaocun;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_account;
private EditText et_password;
private Button btn_login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_account =(EditText)findViewById(R.id.et_account);
et_password =(EditText)findViewById(R.id.et_password);
btn_login =(Button) findViewById(R.id.btn_Login);
btn_login.setOnClickListener(this);//创建监听事件
Map<String,String> map=getUserInfo();
et_account.setText(map.get("账号"));
et_password.setText(map.get("密码"));
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id .btn_Login:
String account=et_account.getText().toString().trim();
String password=et_password.getText().toString().trim();
if (TextUtils.isEmpty(account)){
Toast.makeText(this,"账号为空",Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(password)){
Toast.makeText(this,"密码为空",Toast.LENGTH_SHORT).show();
return;
}else{
Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show();
}
//保存文件
boolean issucess=SaveQQ(account,password);
if(issucess==true){
Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
}
break;
}
}
//内部存储 将数据保存到文件中
private boolean SaveQQ(String account, String password){
SharedPreferences sp=getSharedPreferences("data",MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString("账号",account);
editor.putString("密码",password);
editor.commit();
return true;
}
private Map<String,String> getUserInfo() {
SharedPreferences SP=getSharedPreferences("data",MODE_PRIVATE);
String account=SP.getString("账号","");
String password=SP.getString("密码","");
Map<String,String> map=new HashMap<>();
map.put("账号",account);
map.put("密码",password);
return map;
}
}