在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/下

} android SharePrefereces的使用入门_第1张图片

更多相关文章

  1. Android 数据存储与读取:文件
  2. android 跳转并传递参数
  3. Android 串口数据处理
  4. Android 关于 如何使用外界导入的数据库文件
  5. Android 打造任意层级树形控件 考验你的数据结构和设计
  6. Android - 利用Android studio + Android Killer工具在手机未ROO
  7. android 蓝牙ble app开发(二) -- 关键概念,连接参数,连接请求

随机推荐

  1. AudioRecord||AudioTrack
  2. android scaleType的属性
  3. Android:Error retrieving parent for it
  4. Android:获取APK签名信息
  5. Android(安卓)activity属性设置大全
  6. Android控件属性大全
  7. Android(安卓)NDK开发之Android.mk文件
  8. android 神奇的android:clipChildren属性
  9. Android(安卓)单独抽取 WebRtc-AGC(音频增
  10. 【转】Android(安卓)Makefile中是 如何识