Android编程6:SharedPreferences文件存储操作

来源:互联网 发布:移动硬盘装ubuntu 编辑:程序博客网 时间:2024/05/02 03:02

本文博客链接: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);      }}
效果图

原创粉丝点击