android数据存储之SharedPreferences

本文介绍了SharedPreferences在Android开发中的应用,包括其特点、操作方法及常见问题解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SharedPreferences是SDK中的一个自带类,可以完成简单的数据存储。



一、特点
1.是一种轻量级的数据存储方式
2.本质是基于XML(key-value)的形式存储的
3.通常用于存储一些简单的配置信息,如用户名和密码


      二、操作:
1.读取数据:SharedPreferences的对象来完成
2.更新数据:Editor的接口对象来完成


      三、代码实现:
  1.获取SharedPreferences对象
a) 获取安卓默认的pref对象,会建立/data/data/包名/shared_pref/xxx.xml文件
//参数:上下文对象 
SharedPreferences pref=PreferenceManeger.getDefaultSharedPreferences(this);
b) 自定义获取pref对象 xxx.xml的名称,对该xml的操作权限
SharedPreferences pref=getSharedPreferences("pref",MODE_PRIVATE);
  2.获得Editor对象
a) Editor editor=pref.edit();
  3.更新数据
editor.putString("name","张三");
editor.putInt("age",19);
editor.putBoolean("default",true);
editor.remove("default");


editor.commit();
  4.获得数据
String name=pref.getString("name");
int age=pref.getInt("age");


总结:xml中的数据除了editor.clear()和在手机应用程序中清空数据会一直有效!!!
我在写一个小Demo的时候,在onCreate外面初始化pref一直报空指针异常....!!!

搞了好久才发现,只有onCreate之后,才会有当前(this)对象!!!

MainActivity.java

package com.example.dsd;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class MainActivity extends Activity {

	private EditText mEtInput;
	private TextView mTvShow;
	private Button mBtnSave;
	private Button mBtnShow;
	private Button mBtnClear;

	private SharedPreferences pref;
	private Editor editor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		pref = PreferenceManager.getDefaultSharedPreferences(this);
		editor = pref.edit();

		mEtInput = (EditText) findViewById(R.id.et_input);
		mTvShow = (TextView) findViewById(R.id.tv_show);
		mBtnSave = (Button) findViewById(R.id.btn_save);
		mBtnShow = (Button) findViewById(R.id.btn_show);
		mBtnClear = (Button) findViewById(R.id.btn_clear);

		mBtnSave.setOnClickListener(listener);
		mBtnShow.setOnClickListener(listener);
		mBtnClear.setOnClickListener(listener);
	}

	private OnClickListener listener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btn_save:
				editor.putString("content", mEtInput.getText().toString());
				break;

			case R.id.btn_show:
				mTvShow.setText(pref.getString("content", "您还未存储").trim());
				break;

			case R.id.btn_clear:
				editor.clear();
				break;
			}
			editor.commit();
		}
	};
}

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/et_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存" />

    <Button
        android:id="@+id/btn_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="展示" />

    <Button
        android:id="@+id/btn_clear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="清空" />

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值