安卓实现记住密码登陆功能

在安卓程序中,我们经常会使用到很多登陆的功能,会看到很多记住密码的地方。这里,我给大家写一个简单的记住密码的功能。注意,这里是简单的记住密码功能,仅用于练习学习用哦。这个程序主要是给学习安卓的朋友作为一个入门程序来学习。用到的知识点主要就是JAVA的IO流知识。所以,如果对IO流知识不熟悉的朋友们,一定要好好回去复习一下IO的知识。IO流的知识对于我们以后的开发有着十分重要的地位。这里顺便给点建议,学习安卓其实到头来,难得还是JAVA。如果前期JAVA学的好,其实安卓学起来是还是比较得心应手的。

 

这里,我们先说一下这个程序吧。

程序主要实现的功能就是,用户输入账号和密码,如果用户有勾选记住密码,点击登陆按钮后再退出程序,下一次启动程序的时候,会自动加载账号和密码。如果用户没有勾选就不会加载。

原理:这里我们主要就是把用户输入的账号和密码用IO流写入到info.txt文件中,如果有记住密码,在下一次启动的时候,就再用IO流去读取info.txt中的账号和密码。

 说到文件读写的问题,顺便说一下,安卓中,程序只能对“data/data/自己程序文件夹”这个路径下进行操作。其他程序下的文件夹是无法访问的。

下面我们看看运行截图和代码。我们先看看截图

下面看看代码

布局文件 activity_main.xml

<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="com.example.mylogin.MainActivity" >

    <EditText
        android:id="@+id/et_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/tips" />

    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/tips2"
        android:inputType="textPassword" />

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

        <CheckBox
            android:id="@+id/cb_remerber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:text="@string/tips3" />

        <Button
            android:id="@+id/bt_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:onClick="login"
            android:text="@string/login" >
        </Button>
    </RelativeLayout>

</LinearLayout>

字符串文件 strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">mylogin</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="tips">请输入用户名</string>
    <string name="tips2">请输入用户密码</string>
    <string name="tips3">记住密码</string>
    <string name="login">登陆</string>

</resources>

Java文件MainActivity.java

package com.example.mylogin;

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 android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

    private EditText userName, passWord;
    private CheckBox box;
    File file = null;

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

        userName = (EditText) findViewById(R.id.et_username);
        passWord = (EditText) findViewById(R.id.et_password);
        box = (CheckBox) findViewById(R.id.cb_remerber);

        try {
            load();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // 点击登陆,写入账户密码的方法
    @SuppressLint("ShowToast")
    public void login(View v) throws IOException {
        String name = userName.getText().toString();
        String pwd = passWord.getText().toString();
        FileOutputStream fos = null;
        // 判断是否有勾选记住密码
        if (box.isChecked()) {
            try {
                /*
                 * getFilesDir()路径其实就是data/data/项目包/files 安卓中,每一个程序只能在自己的包下进行读写。
                 * 例如,本例子中,其实路径就是 data/data/com.examle.mylogin/files/info.txt
* 这里补充一点,如果文件要写在sd卡上,那么路径为storage/sdcard/info.txt,注意,写在sd卡是要添加读写权限的。
* 当然咯,路径不用自己写,可以用api获取,Environment.getExternalStorageDirectory()
* android.permission.READ_EXTERNAL_STORAGE,android.permission.WRITE_EXTERNAL_STORAGE
*/ file = new File(getFilesDir(), "info.txt"); fos = new FileOutputStream(file); // 将name和pwd转化为字节数组写入。##是为了方便待会分割 fos.write((name + "##" + pwd).getBytes()); Toast.makeText(MainActivity.this, "登陆成功", 0).show(); } finally { if (fos != null) { fos.close(); } } } else { // 如果用户没有勾选记住密码,就判断file是否存在,存在就删除 if (file.exists()) { file.delete(); Toast.makeText(MainActivity.this, "登陆成功", 0).show(); } else { Toast.makeText(MainActivity.this, "登陆成功", 0).show(); } } } // 加载账户密码的方法 public void load() throws IOException { FileInputStream fiStream = null; BufferedReader br = null; file = new File(getFilesDir(), "info.txt"); if (file.exists()) { try { fiStream = new FileInputStream(file); /* 将字节流转化为字符流,转化是因为我们知道info.txt * 只有一行数据,为了使用readLine()方法,所以我们这里 * 转化为字符流,其实用字节流也是可以做的。但比较麻烦 */ br = new BufferedReader(new InputStreamReader(fiStream)); //读取info.txt String str = br.readLine(); //分割info.txt里面的内容。这就是为什么写入的时候要加入##的原因 String arr[] = str.split("##"); userName.setText(arr[0]); passWord.setText(arr[1]); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (br != null) { br.close(); } } } else { } } }

 

posted @ 2016-04-14 01:47  _Vincent  阅读(8106)  评论(0编辑  收藏  举报