本文博客链接:
http://blog.csdn.net/jdh99
,作者:jdh,转载请注明.
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); } }效果图 :