zoukankan      html  css  js  c++  java
  • 使用SharedPreferences接口来实现记住密码功能

    SharedPreferences接口非常适合用来存储零散的数据。这里我们用来实现记录用户名和密码的功能。在前面我用过IO流来实现记住密码的功能。那么用SharedPreferences接口会比用IO流更加方便,代码更加简洁。也更高效。

    原理:SharedPreferences接口会在xxxx路径下生成xml文件。我们根据xml文件中的key和valu来使用所存储的值。

    下面我们看看运行效果

     

    然后是布局文件

    <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>

    字符串资源文件

    <?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文件

    package com.example.mylogin2;
    
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.CheckBox;
    import android.widget.EditText;
    import android.widget.Toast;
    
    @SuppressLint("CommitPrefEdits")
    public class MainActivity extends Activity {
    
        private EditText userName, passWord;
        private CheckBox box;
    
        @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);
            
            load();
        }
    
        // 点击登陆,写入账户密码的方法
        @SuppressLint("ShowToast")
        public void login(View v) {
            String name = userName.getText().toString();
            String pwd = passWord.getText().toString();
    
            // 判断是否有勾选记住密码
            if (box.isChecked()) {
                /*
                 * getSharedPreferences(),第一个参数是生成info.xml文件,第二是是访问权限
                 * 生成路径/data/data/包名/share_perfs/info.xml
                 */
                //取得SharedPreferences对象
                SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);
                // 取得编辑器
                Editor et = sp.edit();
                // 传入数据,参数的意思是 (key,value)看待会生成的info.xml就明白了
                et.putString("NAME", name);
                et.putString("PWD", pwd);
                // 提交数据
                et.commit();
                Toast.makeText(this, "登陆成功", 0).show();
    
            } else {
                Toast.makeText(this, "登陆成功", 0).show();
            }
        }
    
        // 加载账户密码的方法
        public void load() {
            //取得SharedPreferences对象
            SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);
            /*
             * getString,参数意义表示用key=第一个参数去取值,如果取不到值,就返回第二个参数
             * 因为有了第二个参数,所以我们也不需要判断是否存在info文件
             * */
            String nameString = sp.getString("NAME", "");
            String pwdString = sp.getString("PWD", "");
            
            userName.setText(nameString);
            passWord.setText(pwdString);
        }
    
    }
  • 相关阅读:
    AC日记——色板游戏 洛谷 P1558
    AC日记——方差 洛谷 P1471
    AC日记——[Scoi2010]序列操作 bzoj 1858
    AC日记——Sagheer and Nubian Market codeforces 812c
    AC日记——Sagheer, the Hausmeister codeforces 812b
    AC日记——Sagheer and Crossroads codeforces 812a
    [BJOI2019]排兵布阵 DP
    多重背包二进制优化
    BZOJ 3211 花神游历各国 线段树
    「CQOI2006」简单题 线段树
  • 原文地址:https://www.cnblogs.com/linfenghp/p/5393270.html
Copyright © 2011-2022 走看看