android 保存参数设置,Android之使用SharedPreferences保存用户偏好参数

在Android应用中,我们常需要记录用户设置的一些偏好参数,,此时我们就需要用SharedPreferences和Editor将这些信息保存下来,在下次登录时读取。

SharedPreferences保存的数据主要类似于配置信息格式的数据,因此它保存数据的形式为key-value对,下面我们来看下实例代码。

首先是界面布局,比较简单,就是一个普通的登陆界面.

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MainActivity" >

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/account"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/password"

android:layout_below="@id/account"

/>

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_below="@id/password"

android:text="保存参数"

android:id="@+id/save"

android:onClick="save"

/>

这是自定义的Preferences 类,用来实现数据的保存,可在Android的内置存储空间产生一文件。

import android.R.integer;

import android.content.Context;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.widget.EditText;

public class Preferences {

private Context context;

public Preferences(Context context)

{

this.context=context;

}

public void save(String name, Integer valueOf)

{

//保存文件名字为"shared",保存形式为Context.MODE_PRIVATE即该数据只能被本应用读取

SharedPreferences preferences=context.getSharedPreferences("shared",Context.MODE_PRIVATE);

Editor editor=preferences.edit();

editor.putString("name", name);

editor.putInt("age", valueOf);

editor.commit();//提交数据

}

}

下面是Mainactivity的代码。在activity的oncreate阶段我们加载本地的数据。

import java.util.HashMap;

import java.util.Map;

import android.R.integer;

import android.os.Bundle;

import android.app.Activity;

import android.content.SharedPreferences;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends Activity {

private EditText account,passworad;

Preferences prefer;//自定义的类

SharedPreferences preference;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

account=(EditText)findViewById(R.id.account);

passworad=(EditText)findViewById(R.id.password);

//获取本地的数据

preference=getSharedPreferences("shared", MODE_PRIVATE);

Map map=new HashMap();

map.put("name",preference.getString("name",""));

map.put("age", String.valueOf(preference.getInt("age", 0)));

account.setText(map.get("name"));

passworad.setText(map.get("age"));

}

//保存文件的方法

public void save(View v) {

String name=account.getText().toString();

String age=passworad.getText().toString();

prefer=new Preferences(this);

prefer.save(name,Integer.valueOf(age));

Toast.makeText(getApplicationContext(), "保存完成", Toast.LENGTH_SHORT).show();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

我们看一下效果.

9b49417c478dfb856311dc0727bb22d3.png

点击保存参数,出现保存完成则说明我们已经保存成功了,在下次登录的时候可以看到这些参数还在。因为记录文件是在内置空间中的,所以我们在SD卡中找不到该文件,

如果有root权限的手机可以下载个RE文件管理,我们可以再/data/data/的路径找到很多应用程序的内置文件夹,我们可以在这些文件夹中看到一个shared_prefs文件夹,

里面就有我们刚刚设置而产生的xml文件。

d369bcb67a454e043e748066cc00e36e.png

a81dbab543bf9a3827a93e653b6fdf01.png

0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值