Android使用指南之sharedpreference

Android使用之sharedPreferences

今天在写登录界面,在实现记住密码和自动登陆的时候,用到了sharedPreferences 这里做个小小的总结。主要是最近发现自己老了,不写下来就忘了!有时候觉得人健忘也未必不是件好事对吧。不扯淡了,进入正题。

sharedPreferences 可以用来进行数据的共享,包括应用程序之间,或者同一个应用程序中的不同组件。比如两个activity除了通过Intent传递数据之外,也可以通过SharedPreferences来共享数据。

定义共享偏好

SharedPreferences sharedata = getSharedPreferences(“data”, 0);

存数据

Editor sharedata = getSharedPreferences(“data”, 0).edit();

sharedata.putString(“item”,”hello getSharedPreferences”);

sharedata.commit();

读数据

String data = sharedata.getString(“item”, null);

通过SharedPreferences可以保存程序的某些配置信息,而程序员不需要知道它到底以什么形式保存的,保存在了什么地方。

在Android系统中,SharedPreferences中的信息以XML文件的形式保存在 /data/data/PACKAGE_NAME/shared_prefs目录下。


用户登陆界面

从百度上面copy了那么多,感觉没有什么鸟可以用嘛,事实证明有代码才是王道。毕竟有位帅的一逼的屌丝曾说过这样的一句话:能用代码说话的时候,就别瞎逼逼!——电科彭于晏

这个用户登陆界面的demo,相对比较粗糙,没有加入相对fashion的动画,毕竟那个不是本文的重点,各位老铁莫要觉得扎心,毕竟本人还是比较走心的!

1. 界面实现功能:

1、能够判断用户的账号密码正确后(由于没有加后台服务器,这里只是简单判读程序的默认账号密码,相信在不久的将来本帅会分享给大家包括服务器和数据库在内的相对比较完整的开源程序),才能实现记住密码,和自动登陆的功能;

2、记住密码和自动登录功能,默认的情况下是不选择的;

3、使用上文提到的sharedPreferences实现再次打开应用的时候能根据本地信息自动选择是否去勾选显示相应控件的状态;

4、清空按钮,在没有信息输入的时候清空按钮是不会显示的,在有信息输入的时候才会有显示,点击后自动清空内容;

2. 登陆界面显示效果:

2.1 效果图

不喜勿喷,因为你喷我的话,我会喷回去的!

ogin

2.2 登陆界面代码XML
<?xml version="1.0" encoding="utf-8"?>
<!-- android:focusable="true"
  android:focusableInTouchMode="true"
  把EditText默认的行为截断了! -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android.kimhlo.smartfeedapplication.LoginActivity"
    android:orientation="vertical"
    android:padding="16dp"
    android:background="#ECEDF1"
    android:focusable="true"
    android:focusableInTouchMode="true">

    <ImageView
        android:layout_width="90dp"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:layout_gravity="center"
        android:src="@drawable/ic_account_circle_black_48dp"
        android:id="@+id/account_image"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        android:weightSum="1">

        <!--第一行输入账号-->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <EditText
                android:id="@+id/account_edit_text"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:gravity="center"
                android:hint="@string/account_name_hint"
                android:inputType="textPersonName"
                android:textColor="#bd6c1f"
                android:textColorHint="#CCADAD" />

            <ImageView
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:src="@drawable/ic_account_box_black_24dp" />

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:src="@drawable/ic_clear_black_48dp"
                android:visibility="invisible"
                android:id="@+id/clear_account_name_image_view"
                android:layout_centerVertical="true"
                android:layout_alignParentEnd="true" />
        </RelativeLayout>

        <!--第二行输入密码-->
        <RelativeLayout
         
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值