Android使用SharedPreferences实现登录帐号和密码的保存方法简介

本文介绍了如何在Android应用中使用SharedPreferences来保存登录时的账号和密码,包括用户未输入、记住密码和读取密码的功能实现,通过代码展示了具体操作步骤。

先来看看程序运行图:

1.用户未输入状态:


2.用户单击记住密码:


3.用户单击读取密码:


接下来我们来看实现代码:

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

    //声明SharedPreferences对象。
    SharedPreferences sharedPreferences;
    //声明Editor对象。
    SharedPreferences.Editor editor;

    private Button remberButton;
    private Button readButton;
    private EditText studentNumber;
    private EditText passWord;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        remberButton=(Button)findViewById(R.id.remberButton);
        readButton=(Button)findViewById(R.id.readButton);
        studentNumber=(EditText)findViewById(R.id.studentNumber);
        passWord=(EditText)findViewById(R.id.passWord);
        //SharedPreferences本身是一个接口,程序无法直接创建实例因此只能通过Context提供的getSharedPreferences方法来获取SharedPreferences实例。
        /*该方法的第二个参数有三个值分别为:
        * 1.Context.MODE_PRIVATE:指定该SharedPreferences数据只能被本程序访问。
        * 2.Context.MODE_WORLD_READABLE:指定数据可被其他应用读取但不能写入。
        * 3.Context.MODE_WORLD_WRITEABLE:指定数据可以被其他应用读写。
        * 注意:
        * 从Android 4.2开始Android官方并不再推荐使用Context.MODE_WORLD_READABLE,Context.MODE_WORLD_WRITEABLE
        * 这两种方式因为这两种方式允许外来应用访问他人的软件会造成安全漏洞。
        *
        *
        * */
        sharedPreferences=getSharedPreferences("studentList", Context.MODE_PRIVATE);
        editor=sharedPreferences.edit();
        readButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //使用sharedPreferences.getString()的方法来访问保存的数据。
                String studentNumber=sharedPreferences.getString("studentNumber",null);
                String passWord=sharedPreferences.getString("passWord",null);
                if(studentNumber==null)
                {
                    Toast.makeText(MainActivity.this,"当前没有学生记录请添加!",Toast.LENGTH_LONG).show();
                }else
                {
                    Toast.makeText(MainActivity.this,"学号:"+studentNumber+"\n"+"密码:"+passWord,Toast.LENGTH_LONG).show();
                }
            }
        });
        remberButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //写入数据使用editor.putXxx()这里可以指定写入的数据类型。
                editor.putString("studentNumber",studentNumber.getText().toString());
                editor.putString("passWord",passWord.getText().toString());
                //提交数据。
                editor.commit();
                Toast.makeText(MainActivity.this,"成功记录密码!!!",Toast.LENGTH_SHORT).show();
            }
        });

    }

}

下面是布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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: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.zhangyi.sharedpreferences_test.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="学号:"
        android:id="@+id/textView"
        android:textSize="25sp"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="100dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/studentNumber"
        android:hint="请输入学号..."
        android:layout_alignBottom="@+id/textView"
        android:layout_toEndOf="@+id/textView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码:"
        android:textSize="25sp"
        android:id="@+id/textView2"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true"
        android:layout_marginTop="45dp" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:hint="请输入密码..."
        android:id="@+id/passWord"
        android:layout_alignBottom="@+id/textView2"
        android:layout_toEndOf="@+id/textView2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:id="@+id/remberButton"
        android:layout_below="@+id/passWord"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="109dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="读取密码"
        android:id="@+id/readButton"
        android:layout_below="@+id/remberButton"
        android:layout_alignStart="@+id/remberButton"
        android:layout_marginTop="39dp" />
</RelativeLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值