当前位置: 移动技术网 > 移动技术>移动开发>Android > Android编程之SharedPreferences文件存储操作实例分析

Android编程之SharedPreferences文件存储操作实例分析

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了android编程之sharedpreferences文件存储操作的方法。分享给大家供大家参考。具体分析如下:

sharedpreferences类提供了一种简单的文件存储功能,像程序的配置文件可以通过它来实现。

源代码:

package com.test.sharedpreferences; 
import android.app.activity; 
import android.content.context; 
import android.content.sharedpreferences; 
import android.os.bundle; 
import android.widget.textview; 
public class test_sharedpreferences extends activity 
{ 
 sharedpreferences file; 
 sharedpreferences.editor editor; 
 textview text; 
 /** called when the activity is first created. */ 
 @override 
 public void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.main); 
  //获得配置文件 
  file = getsharedpreferences("test_config", 0); 
  editor = file.edit(); 
  //写入配置:id:10000 name:jdh 
  editor.putint("id", 10000); 
  editor.putstring("name", "jdh"); 
  editor.commit(); 
  //读取配置 
  int id = 0; 
  id = file.getint("id", 0); 
  string str; 
  str = file.getstring("name", null); 
  text = (textview)findviewbyid(r.id.textview1); 
  text.settext("id:" + id + " name:" + str); 
 } 
}

效果图如下:

希望本文所述对大家的android程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网