登录案例

UI布局

这里写图片描述

<?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"
    tools:context=".MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:id="@+id/et_account"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入账号" />

        <EditText
            android:id="@+id/et_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:hint="请输入密码"
            android:inputType="textPassword" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="10dp">
            <CheckBox
                android:id="@+id/cb_remberAccount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="记住用户名和密码"
                android:layout_alignBaseline="@id/bt_login"/>
            <Button
                android:id="@+id/bt_login"
                android:onClick="click"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="登录"/>
        </RelativeLayout>
    </LinearLayout>


</android.support.constraint.ConstraintLayout>

android studio查看设备上目录

这里写图片描述

保存数据

将数据保存到txt中

package com.example.administrator.logincase;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class userInfo {
    public boolean saveInfo(String strAccount, String strPassword) {
        String result = strAccount + "##" + strPassword;
        File file = new File("/data/data/com.example." +
                "administrator.logincase/info.txt");
        try {
            FileOutputStream fos=new FileOutputStream(file);
            try {
                fos.write(result.getBytes());
                fos.close();
                return true;
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }
}


package com.example.administrator.logincase;

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;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void click(View v) {
        EditText et_account=findViewById(R.id.et_account);
        EditText et_password=findViewById(R.id.et_password);
        CheckBox et_remerberAccount=findViewById(R.id.cb_remberAccount);
        String strAccount=et_account.getText().toString().trim();
        String strPassword=et_password.getText().toString().trim();
        if(TextUtils.isEmpty(strAccount) || TextUtils.isEmpty(strPassword)){
            Toast.makeText(this,"用户名和密码不能为空",Toast.LENGTH_SHORT).show();
        }
        else {
            if(et_remerberAccount.isChecked()) {
                userInfo info=new userInfo();
                boolean bSuccess=info.saveInfo(strAccount,strPassword);
                if(bSuccess){
                    Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show();
                }
                else{
                    Toast.makeText(this,"保存失败",Toast.LENGTH_SHORT).show();
                }
            }
            else {
                Toast.makeText(this,"未保存用户名和密码",Toast.LENGTH_SHORT).show();
            }
        }
    }
}

从文件中读取用户名和密码

 public Map<String, String> readInfo() {
        File file = new File("/data/data/com.example." +
                "administrator.logincase/info.txt");
        try {
            Map<String, String> maps = new HashMap<String, String>();
            FileInputStream fis = new FileInputStream(file);
            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
            String info = reader.readLine();
            String[] infos = info.split("##");
            maps.put("account", infos[0]);
            maps.put("password", infos[1]);
            return maps;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        readConfig();
    }

    private void readConfig() {
        userInfo info = new userInfo();
        Map<String, String> infos = info.readInfo();
        if (infos != null) {
            EditText et_account = findViewById(R.id.et_account);
            EditText et_password = findViewById(R.id.et_password);
            et_account.setText(infos.get("account"));
            et_password.setText(infos.get("password"));
        }
    }

使用上下文获取常用目录

通过上下文类可以直接获取到应用程序的相关环境,包括数据的存储目录
userinfo类

package com.example.administrator.logincase;

import android.content.Context;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class userInfo {
    public boolean saveInfo(Context context, String strAccount, String strPassword) {
        try {
            FileOutputStream fos = context.openFileOutput("info.txt", Context.MODE_PRIVATE);
            String result = strAccount + "##" + strPassword;
            fos.write(result.getBytes());
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public Map<String, String> readInfo(Context context) {
        try {
            FileInputStream fis = context.openFileInput("info.txt");
            BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
            Map<String, String> maps = new HashMap<String, String>();
            String info = reader.readLine();
            String[] infos = info.split("##");
            maps.put("account", infos[0]);
            maps.put("password", infos[1]);
            return maps;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

使用SharedPreferences

package com.example.administrator.logincase;

import android.content.SharedPreferences;
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.util.Map;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        readConfig();
    }

    private void readConfig() {

        EditText et_account = findViewById(R.id.et_account);
        EditText et_password = findViewById(R.id.et_password);

        SharedPreferences sp = getSharedPreferences("accountInfo", MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        sp.getString("account", "");
        et_account.setText(sp.getString("account", ""));
        et_password.setText(sp.getString("password", ""));

    }

    public void click(View v) {
        EditText et_account = findViewById(R.id.et_account);
        EditText et_password = findViewById(R.id.et_password);
        CheckBox et_remerberAccount = findViewById(R.id.cb_remberAccount);
        String strAccount = et_account.getText().toString().trim();
        String strPassword = et_password.getText().toString().trim();
        if (TextUtils.isEmpty(strAccount) || TextUtils.isEmpty(strPassword)) {
            Toast.makeText(this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
        } else {
            if (et_remerberAccount.isChecked()) {
                SharedPreferences sp = getSharedPreferences("accountInfo", MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("account", strAccount);
                editor.putString("password", strPassword);
                editor.commit();
                Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "未保存用户名和密码", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/kebiaoy/article/details/81048770