zoukankan      html  css  js  c++  java
  • sharepreference实现记住password功能

        SharePreference是用于保存数据用的。主要调用Context.getSharePreferences(String name, int mode)方法来得到SharePreferences接口,该方法的第一个參数是文件名。第二个參数是操作模式。

    操作模式有三种:


    MODE_PRIVATE(私有) 

    MODE_WORLD_READABLE(可读)

    MODE_WORLD_WRITEABLE(可写)


        SharePreference提供了获得数据的方法。如getString(String key,String defValue)等。调用harePreferences的edit()方法返回SharePreferences.Editor内部接口。该接口提供了保存数据的方法如:putString(String key,String value)等,调用该接口的commit()方法能够将数据保存。


    效果图例如以下:





    主要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:background="@drawable/bg_login_activity"
        android:orientation="vertical"
        tools:context=".MainActivity" >
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="35dip"
            android:layout_marginTop="150dip"
            android:orientation="horizontal" >
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="用户名:"
                android:textSize="20dp" />
    
            <EditText
                android:id="@+id/username"
                android:layout_width="200dp"
                android:layout_height="35dp"
                android:background="@drawable/bg_input_center" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="35dip"
            android:layout_marginTop="8dp"
            android:orientation="horizontal" >
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="    密码:"
                android:textSize="20dp" />
    
            <EditText
                android:id="@+id/password"
                android:layout_width="200dp"
                android:layout_height="35dp"
                android:background="@drawable/bg_input_center"
                android:password="true" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="75dip"
            android:layout_marginTop="8dp"
            android:orientation="horizontal" >
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="记住密码:" />
    
            <CheckBox
                android:id="@+id/savePassword"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
        </LinearLayout>
    
        
        <Button
            android:id="@+id/login_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="75dip"
            android:text="登陆" />
        
        </LinearLayout>
        
    </LinearLayout>

    保存数据到文件的主要函数:


    public void setUserInfo(String key, String value) {
              SharedPreferences sp = context.getSharedPreferences(USER_INFO,
                      Context.MODE_PRIVATE);
              SharedPreferences.Editor editor = sp.edit();
              editor.remove(key);
              editor.putString(key, value);
     26         editor.commit();
     27     }






  • 相关阅读:
    mysql 历史版本下载
    mysql 5.7 版本 You must reset your password using ALTER USER statement before executing this statement报错处理
    5.7 zip 版本的安装 以及遇到的坑
    mysql 5.6zip版本的卸载与5.7 zip 版本的安装
    mysql数据库的备份与还原
    本地Navicat连接docker里的mysql
    docker修改数据库密码
    docker 在push镜像到本地registry出现的500 Internal Server Error
    linux 没有界面内容显示不全解决办法
    json与map互相转换
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/5230444.html
Copyright © 2011-2022 走看看