Android 模拟登陆 保存密码(信息)到手机中 文件信息读取

package com.wuyou.login;

import java.io.IOException;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.wuyou.service.LoginService;

public class MainActivity extends Activity {

    private CheckBox checkBox;
    private EditText usernamEditText;
    private EditText passwordeEditText;

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

        checkBox = (CheckBox) this.findViewById(R.id.cb);
        usernamEditText = (EditText) this.findViewById(R.id.username);
        passwordeEditText = (EditText) this.findViewById(R.id.password);

        // 将之前保存在本地的账号密码取出并设置到文本框中
        try {
            Map<String, String> map = LoginService.getInfo(this);
            usernamEditText.setText(map.get("username"));
            passwordeEditText.setText(map.get("password"));

        } catch (Exception e) {
            Log.i("main", "取不出账号密码");
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    /**
     * 点击“登陆”按钮触发的时间
     * 
     * @param v
     */
    public void login(View v) {
        Log.i("login", "在登陆中");
        String username = usernamEditText.getText().toString().trim();
        String password = passwordeEditText.getText().toString().trim();
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
            Toast.makeText(this, "用户名和账号不能为空!", Toast.LENGTH_SHORT).show();
        } else {
            // 判断是否选择了记住密码
            if (checkBox.isChecked()) {
                try {
                    LoginService.saveInfo(this, username, password);
                    Toast.makeText(this, "记住密码成功!", Toast.LENGTH_SHORT).show();
                } catch (IOException e) {
                    Toast.makeText(this, "无法记住密码", Toast.LENGTH_SHORT).show();
                }
            }
            if ("zhangsan".equals(username) && "123".equals(password)) {
                Toast.makeText(this, "登陆成功!", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, "账号密码有误!", Toast.LENGTH_SHORT).show();
            }
        }

    }

}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入账号" />

    <EditText
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="请输入密码" />

    <EditText
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <CheckBox
            android:id="@+id/cb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="记住密码" />

        <!-- 记住login的方法有一个参数View -->
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:onClick="login"
            android:text="登陆" />
    </RelativeLayout>

</LinearLayout>
package com.wuyou.service;

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

import android.content.Context;

public class LoginService {

    public static void saveInfo(Context context, String username,
            String password) throws IOException {
        File file = new File(context.getFilesDir(), "info.txt");
        FileWriter fileWriter = new FileWriter(file);
        fileWriter.write(username + "##" + password);
        fileWriter.close();
    }

    public static Map<String, String> getInfo(Context context)
            throws IOException {
     //这里只做简单的保存,如果要保存配置文件信息,可以使用SharedPreference类保存,请关注本类别的该文章 File file
= new File(context.getFilesDir(),"info.txt"); Map<String, String> map = new HashMap<String, String>(); BufferedReader bf = new BufferedReader(new InputStreamReader( new FileInputStream(file))); String info = bf.readLine(); String username = ""; String password = ""; if (info != null) { String[] infos = info.split("##"); username = infos[0]; password = infos[1]; map.put("username", username); map.put("password", password); } bf.close(); return map; } }
原文地址:https://www.cnblogs.com/wuyou/p/3416893.html