在Android我們可以通過shareprefereces來處理配置文件的數據
package com.android;
import android.R.string;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class prefereceTest extends Activity {
/** Called when the activity is first created. */
int i=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//創建SharePrefereces
SharedPreferences sp = getSharedPreferences("myPref",MODE_WORLD_READABLE);//第一個參數為文件名,第二個為數據操作模式
Editor editor = sp.edit();
//添加數據
editor.putString("name", "mypreferece");
editor.putInt("int", 5566);
//保存數據
editor.commit();
Button bt = (Button)findViewById(R.id.bt_1);
bt.setOnClickListener(new onclicklistener());
}
class onclicklistener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
//創建SharePrefereces
SharedPreferences sp = getSharedPreferences("myPref",MODE_WORLD_READABLE);
//獲取數據後面一個參數表示無返回時的默認值
String defaultval = "defaultval";
String name = sp.getString("name", defaultval);
int defint =0;
int age = sp.getInt("int", defint);
TextView tv = (TextView) findViewById(R.id.tv);
//toasst顯示數據
Toast.makeText(getApplicationContext(), age + " "+name,Toast.LENGTH_SHORT).show();
}
}
文件位於 、data/data/包名/shared_pref/下
}