在Android中使用SharedPreferences保存简单的数据
2015-05-08 22:43
453 查看
在Android中保存一些简单的数据,使用SharedPreferences是比较方便的。比如用SharedPreferences保存软件的配置文件等,SharedPreferences实质是把数据写在一个xml文件中。具体实现代码如下; xml布局文件
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" >
<button< span="">
android:id="@+id/btnsave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save"
android:onClick="SaveBtn"
/>
<button< span="">
android:id="@+id/btnread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnsave"
android:text="@string/read"
android:onClick="ReadBtn"
/>
本例中使用在布局文件中指定onClick方法响应点击事件,以下是java代码:
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void SaveBtn(View v){ //保存数据
SharedPreferences sp = this.getSharedPreferences("config", Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("username", "用户名:55484946");
editor.putString("password", "密码:12345678");
editor.commit();
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show();
}
public void ReadBtn(View v){ //读取数据
SharedPreferences sp = this.getSharedPreferences("config", Context.MODE_PRIVATE);
String str = sp.getString("username", "用户名不存在");
str += sp.getString("password", null);
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
}
}
以上就是SharedPreferences 保存数据的实现。
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" >
<button< span="">
android:id="@+id/btnsave"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save"
android:onClick="SaveBtn"
/>
<button< span="">
android:id="@+id/btnread"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnsave"
android:text="@string/read"
android:onClick="ReadBtn"
/>
本例中使用在布局文件中指定onClick方法响应点击事件,以下是java代码:
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void SaveBtn(View v){ //保存数据
SharedPreferences sp = this.getSharedPreferences("config", Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString("username", "用户名:55484946");
editor.putString("password", "密码:12345678");
editor.commit();
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show();
}
public void ReadBtn(View v){ //读取数据
SharedPreferences sp = this.getSharedPreferences("config", Context.MODE_PRIVATE);
String str = sp.getString("username", "用户名不存在");
str += sp.getString("password", null);
Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
}
}
以上就是SharedPreferences 保存数据的实现。
相关文章推荐
- Android数据存储之SharedPreferences的简单使用(保存用户名和密码)
- Android 使用Application类保存应用的全局数据
- Android 使用Application类保存应用的全局数据
- 在AndroidManifest中使用meta-data保存数据完整示例
- 在AndroidManifest中使用meta-data保存数据完整示例
- Android编程心得-使用Application类保存应用的全局数据
- Android App中各种数据保存方式的使用实例总结
- android使用Jsoup进行html数据解析简单实例浅析
- Android-02:使用SharedPreferences存储简单数据
- 【android, 3】3.操作数据保存到rom,sd卡上,sharedpreference的使用,pul解析xml
- android使用sharepreference保存用户操作数据
- (十)android 中数据存储与访问——使用SharedPreferences保存数据
- Android中JSON数据格式的简单使用
- Android中XML格式数据的简单使用
- 【Java/Android性能优 4】PreloadDataCache支持预取的数据缓存,使用简单,支持多种缓存算法,支持不同网络类型,扩展性强
- 无废话Android之android下junit测试框架配置、保存文件到手机内存、android下文件访问的权限、保存文件到SD卡、获取SD卡大小、使用SharedPreferences进行数据存储、使用Pull解析器操作XML文件、android下操作sqlite数据库和事务(2)
- android数据存储_SharedPreferences的简单使用
- Android使用SharedPreferences保存数据实例
- Android中webservice的简单使用以及使用handle在UI线程和子线程中传递数据
- 【Android进阶】如何使用文件来保存程序中的数据