sharepreference实现记住密码功能

    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     }






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值