本文实例讲述了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程序设计有所帮助。
如对本文有疑问, 点击进行留言回复!!
Codeforces C. A Cookie for You (模拟 / 分类 / 贪心) (Round #654 Div.2)
DialogFragment弹窗(带黑色透明背景和不带黑色透明背景)
网友评论