android存储方式之SharedPreferences

来源:互联网 发布:手机温度监控软件 编辑:程序博客网 时间:2024/05/18 08:39

在许多的App中都拥有记住密码和账号的功能,对于android开发而言是个相当基础的技术,本文主要采用SharedPreferences的本地存储方式记住密码和用户名;SharedPreferences作为安卓安开发中的主要存储方式,用到的方面比较多 。


下面的是布局代码,就不做详细介绍了

<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_marginTop="80dp" >        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="match_parent" >            <TextView                android:id="@+id/tv_username"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:layout_marginTop="14dp"                android:text="用户名"                android:textColor="#595959"                android:textSize="16sp" />            <EditText                android:id="@+id/et_username"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:layout_marginLeft="20dp"                android:layout_toRightOf="@+id/tv_username"                android:background="@drawable/login_input_end"                android:hint="请输入用户名"                android:textSize="13sp" />        </RelativeLayout>    </LinearLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        >        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="match_parent" >            <TextView                android:id="@+id/tv_password"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:layout_marginTop="14dp"                android:text="密    码"                android:textColor="#595959"                android:textSize="16sp" />            <EditText                android:id="@+id/et_password"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:layout_marginLeft="20dp"                android:layout_toRightOf="@+id/tv_password"                android:background="@drawable/login_input_end"                android:hint="请输入密码"                android:textSize="13sp" />        </RelativeLayout>    </LinearLayout><RelativeLayout     android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_marginTop="50dp"    >    <Button         android:layout_width="fill_parent"        android:layout_height="40dp"        android:text="登    录"        android:id="@+id/btn_login"        android:textSize="17sp"        android:textColor="#FFFFFF"        android:background="@drawable/new_search_button"        android:layout_marginLeft="10dp"        android:layout_marginRight="10dp"/>    <TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="没有账号?"        android:textColor="#595959"        android:textSize="18sp"        android:layout_marginTop="250dp"        android:layout_marginLeft="30dp"        android:id="@+id/tv_register"        />    <Button         android:layout_height="wrap_content"        android:layout_width="90dp"        android:text="直接登录"        android:textColor="#595959"        android:textSize="18sp"        android:background="@drawable/bg_round_rectangle"        android:id="@+id/btn_login_bottom"        android:layout_marginTop="240dp"        android:layout_alignParentRight="true"        android:layout_marginRight="40dp"        /></RelativeLayout></LinearLayout>


这个是java的实现代码

<pre name="code" class="java">package com.zhidi.app;import com.appproject.R;import com.zhidi.activity_1.Mainactivity;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class login_activity extends BActivity implements OnClickListener{public Button btn_login;public EditText et_username,et_password;public Intent intent;String username;String password;/**这个是初始化方法,密码和用户名的自动加载就是在这个方法里实现的*/@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.login);//实例化方法init();//先创建SharedPreferences 对象,通过标记,获取存储的信息(“login”是存储中的标记,就是集合里所说的键,后面是权限)//特别需要提醒的是,在继承activity的情况下可以通过下面的代码获取信息,但是如果继承的是Fragment下面的方法就无效了。//SharedPreferences sp = getActivity().getSharedPreferences("data", getActivity().MODE_PRIVATE),此时把获取上下文的getactivity提到前面SharedPreferences sp = getSharedPreferences("login",getApplicationContext().MODE_PRIVATE);//这里也是根据键,取出对应的信息String username = sp.getString("username","");String password = sp.getString("password","");//将取出的信息放进文本编辑框内,就实现了自动填写信息et_password.setText(password);et_username.setText(username);}//控件的实例化public void init(){btn_login=(Button) findViewById(R.id.btn_login);et_password=(EditText) findViewById(R.id.et_password);et_username=(EditText) findViewById(R.id.et_username);btn_login.setOnClickListener(this);}/* * 信息的存储是在onstop方法中进行的,这个是activity的生命周期中的内容,很重要 * */@Overrideprotected void onStop() {// TODO Auto-generated method stubSharedPreferences sp=getSharedPreferences("login",getApplicationContext().MODE_PRIVATE);//创建编辑对象:信息有了,但是必须的进行编辑SharedPreferences.Editor editor = sp.edit(); editor.putString("username",username);editor.putString("password",password);//编辑结束editor.commit();super.onStop();}//监听事件@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubswitch (arg0.getId()) {case R.id.btn_login://通过点击登录按钮,实现页面的跳转;intent=new Intent(this,Mainactivity.class);startActivity(intent);break;default:break;}}


SharedPreferences在这个程序里是在同一个页面进行的。当然在不同的界面来说也是适用的,这个大家可以自己试试,上面刚才说到的SharedPreferences在在Fragment与activity中进行数据的调用,才会把上下文方式进行调整,同一页面是不需要的










1 0