SharedPreferences

数据会存储在xml里面,存放的路径是/data/data/<包名>/shared_perfs/"指定的sharedPreferences的xml名称,卸载软件以后文件也跟着删除"。

系统提供了五种数据类型:
int、Long、float、boolean、String
数据的操作:
Int:
putInt(String key,int value);//通过SharedPreferences.Edit()接口存放数据。
getInt(String key,int value);//通过key得到int类型的数据。

Float:
putFloat(String key,float value);//通过SharedPreferences.Edit()接口存放数据。
getFloat(String key,float value);//通过key得到float类型的数据。
Long:

putLong(String key,Long value);//通过SharedPreferences.Edit()接口存放数据。
getLong(String key,Longvalue);//通过key得到Lont类型的数据。
boolean:

putboolean(String key,boolean value);//通过SharedPreferences.Edit()接口存放数据。
getboolean(String key,boolean value);//通过key得到boolean类型的数据。

String :

putString (String key,String value);//通过SharedPreferences.Edit()接口存放数据。
getString (String key,String value);//通过key得到String 类型的数据。



代码演示:


package com.yinuo.main;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SharedPf extends Activity {
private LinearLayout linearLayout;
private Button button,button2;
private TextView textView;
private SharedPreferences sharedPf;
private Editor editor;
public void onCreate(Bundle onStateInstance){
super.onCreate(onStateInstance);

sharedPf=getSharedPreferences("yinuo", Context.MODE_PRIVATE);
editor=sharedPf.edit();//得到编辑对象。
linearLayout=new LinearLayout(this);
linearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
linearLayout.setOrientation(LinearLayout.VERTICAL);
showLayout(createSharedpf(editor));
showLayout(readSharedpf(sharedPf));
show();

}
public Button createSharedpf(Editor editor){
this.editor=editor;
button=new Button(this);
button.setText("input");
button.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
SharedPf.this.editor.putString("xingming", "0");
SharedPf.this.editor.putInt("nianling",23);
SharedPf.this.editor.putBoolean("istrue", true);
SharedPf.this.editor.putFloat("money", 43.0f);
SharedPf.this.editor.putLong("long", 100000);
SharedPf.this.editor.commit();//提交
}
});
return button;
}
public Button readSharedpf(SharedPreferences sharedpf){
sharedPf=(SharedPreferences) sharedpf;
button2=new Button(this);
button2.setText("read");
button2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
button2.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
Boolean is=sharedPf.getBoolean("istrue", false);
String str=sharedPf.getString("xingming", "");
int in=sharedPf.getInt("nianling", 0);
long ln=sharedPf.getLong("long",0);
float ft=sharedPf.getFloat("money",2f);
addViews("Boolean:"+is);
addViews("String:"+str);
addViews("int:"+in);
addViews("long:"+ln);
addViews("float:"+ft);
showLayout(getView());
show();

}
});
return button2;
}
public void addViews(String content)
{

textView=new TextView(this);
textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
textView.setText(content);


}
public View getView(){
return textView;
}
public void showLayout(View view){

linearLayout.addView(view);
}
public void show(){

setContentView(linearLayout);
}

}

更多相关文章

  1. 一句话锁定MySQL数据占用元凶
  2. Android(安卓)抽象回调函数以及接口回调更新UI
  3. 总结 使用PHP作为中间介来实现android链接远程数据库。
  4. Android(安卓)常用开发类库
  5. android学习记录2(日志、上下文、android下数据存储、xml基础)
  6. Android-telephony各文件解释
  7. Android(安卓)响应式编程框架 - RxJava2(三)RxJava2+Retrofit 2获
  8. listview 设置数组为空
  9. Android使用内容提供者方式进行存储

随机推荐

  1. PHP接口和抽象类|接口与抽象类的区别与联
  2. 安装Win7系统之后键盘鼠标不能用/失灵没
  3. 数组的排序
  4. PHP高级编程函数的返回值和闭包与作用域
  5. OSG第一课学习
  6. [牛客习题]逆置字符串
  7. 最新网络安全指南,2021年你值得收藏的100
  8. PHP的接口(interface)和抽象类(abstract)
  9. Python学习系列之七大数据类型
  10. 强大:MyBatis 流式查询