当前位置: 代码迷 >> Android >> SharedPreferences简略使用
  详细解决方案

SharedPreferences简略使用

热度:77   发布时间:2016-04-27 22:06:23.0
SharedPreferences简单使用

<?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="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="logins" android:textSize="15sp" /> <EditText android:id="@+id/ed_login_name" android:layout_width="match_parent" android:hint="输入用户名" android:singleLine="true" android:layout_height="wrap_content" android:gravity="center" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:orientation="horizontal" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="passd" android:textSize="15sp" /> <EditText android:id="@+id/ed_login_password" android:layout_width="match_parent" android:hint="输入密码" android:singleLine="true" android:layout_height="wrap_content" android:gravity="center" android:inputType="textPassword" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="horizontal" > <CheckBox android:id="@+id/cb_login_dian" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:text="点击保存密码" android:textSize="12sp" /> <Button android:id="@+id/bb_login_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="5dp" android:text="登陆" android:textSize="13sp" /> </RelativeLayout></LinearLayout>

 

package org.xml.demo.viewpager;import ogg.huanxin.huadong.R;import android.app.Activity;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;import android.view.View;import android.view.Window;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;/* * 使用SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data/<package name>/shared_prefs目录下:  */public class Login extends Activity {    /** 定义登录姓名的编辑框 */    private EditText ed_Name;    /** 定义输入密码的编辑框 */    private EditText ed_PassWord;    /** 定义登录的按钮 当点击时跳转到其他的界面 */    private Button loginButton;    /** 定义个选择框点击时会记住密码 */    private CheckBox cb_dian;    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        super.setContentView(R.layout.login);        ed_Name = (EditText) super.findViewById(R.id.ed_login_name);        ed_PassWord = (EditText) super.findViewById(R.id.ed_login_password);        loginButton = (Button) super.findViewById(R.id.bb_login_login);        cb_dian = (CheckBox) super.findViewById(R.id.cb_login_dian);        loginButton.setOnClickListener(new MyOnClickItem());        /** 定义个方法当下次进来的时候会读取getSharedPreferences若有则将姓名和密码返回到界面上 */        readLogin();    }    private class MyOnClickItem implements View.OnClickListener {        @Override        public void onClick(View arg0) {            // TODO Auto-generated method stub            if (cb_dian.isChecked()) {                BaoCuenLogin();                Toast.makeText(Login.this, "登陆成功", Toast.LENGTH_SHORT).show();            }            Intent it = new Intent(Login.this, DialogText.class);            startActivity(it);        }    }    /** 定义个方法当下次进来的时候会读取getSharedPreferences若有则将姓名和密码返回到界面上 */    private void readLogin() {        SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);        this.ed_Name.setText(sp.getString("name", ""));        this.ed_PassWord.setText(sp.getString("password", ""));    }    /** 实现文件的保存 */    private void BaoCuenLogin() {        String name = ed_Name.getText().toString();        String passWord = ed_PassWord.getText().toString();        SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);        SharedPreferences.Editor ed = sp.edit();        ed.putString("name", name);        ed.putString("password", passWord);        ed.commit();// 提交    }}

然后导出shared_prefs文件夹下的info.xml文件

 android.content.SharedPreferences是一个接口,用来获取和修改持久化存储的数据。有三种获取系统中保存的持久化数据的方式:

        1). public SharedPreferences getPreferences (int mode)
通过Activity对象获取,获取的是本Activity私有的Preference,保存在系统中的xml形式的文件的名称为这个Activity的名字,因此一个Activity只能有一个,属于这个Activity。
        2). public SharedPreferences getSharedPreferences (String name, int mode)
因为Activity继承了ContextWrapper,因此也是通过Activity对象获取,但是属于整个应用程序,可以有多个,以第一参数的name为文件名保存在系统中。
        3). public static SharedPreferences getDefaultSharedPreferences (Context context)
PreferenceManager的静态函数,保存PreferenceActivity中的设置,属于整个应用程序,但是只有一个,Android会根据包名和PreferenceActivity的布局文件来起一个名字保存。
        通过以上方式取得SharedPreferences后就可以对数据进行读取或者保存了。

  相关解决方案