android 存储简要分析

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);
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值