读写SharedPreferences中的数据
来源:互联网 发布:js教程pdf 编辑:程序博客网 时间:2024/06/06 11:40
1.创建Android工程
Project name:sharedPreferences
BuildTarget:Android2.2
Application name:软件参数设置
Package name:com.com.jbridge.pres.activity
Create Activity:PreferencesActivity
Min SDK Version:8
2.编辑strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, PreferencesActivity!</string> <string name="app_name">软件参数设置</string> <string name="tv_name">姓名</string> <string name="tv_age">年龄</string> <string name="bt_write">设置</string> <string name="bt_read">读取</string> <string name="save_success">保存成功</string> <string name="save_failed">保存失败</string> </resources>
3.编辑main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tv_name" android:id="@+id/tv_name" android:textSize="30px" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/et_name" android:layout_toRightOf="@id/tv_name" android:layout_alignTop="@id/tv_name" android:layout_marginLeft="30px" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/tv_age" android:id="@+id/tv_age" android:textSize="30px" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/et_age" android:layout_toRightOf="@id/tv_age" android:layout_alignTop="@id/tv_age" android:layout_marginLeft="30px" /> </RelativeLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt_write" android:text="@string/bt_write" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/bt_read" android:text="@string/bt_read" android:layout_toRightOf="@id/bt_write" android:layout_alignTop="@id/bt_write" android:layout_marginLeft="30px" /> </RelativeLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/tv_show" /> </LinearLayout>
4.为按钮添加事件代码
package com.jbridge.pres.activity; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.content.res.Resources.NotFoundException; import android.os.Bundle; import android.text.Editable; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class PreferencesActivity extends Activity { private EditText etName; private EditText etAge; private View.OnClickListener onClickListener=new View.OnClickListener() { public void onClick(View view) { Button button=(Button) view; SharedPreferences sharedPreferences=PreferencesActivity.this.getSharedPreferences("zyj", Context.MODE_PRIVATE); switch (button.getId()) { case R.id.bt_write: try { Editor editor=sharedPreferences.edit(); editor.putString("name", etName.getText().toString()); editor.putInt("age",Integer.valueOf(etAge.getText().toString()) ); editor.commit(); Toast.makeText(PreferencesActivity.this, R.string.save_success, Toast.LENGTH_LONG).show(); } catch (NumberFormatException e) { Toast.makeText(PreferencesActivity.this, R.string.save_failed, Toast.LENGTH_LONG).show();; e.printStackTrace(); } catch (NotFoundException e) { Toast.makeText(PreferencesActivity.this, R.string.save_failed, Toast.LENGTH_LONG).show();; e.printStackTrace(); } break; case R.id.bt_read: String name=sharedPreferences.getString("name", "no name"); String age=String.valueOf(sharedPreferences.getInt("age", 0)); TextView tvShow=(TextView) PreferencesActivity.this.findViewById(R.id.tv_show); tvShow.setText("姓名: "+name+" 年龄: "+age); break; default: break; } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); SharedPreferences sharedPreferences=PreferencesActivity.this.getSharedPreferences("zyj", Context.MODE_PRIVATE); etName=(EditText) this.findViewById(R.id.et_name); etAge=(EditText) this.findViewById(R.id.et_age); etName.setText(sharedPreferences.getString("name", "no name")); etAge.setText(String.valueOf(sharedPreferences.getInt("age", 0))); Button btWrite=(Button) this.findViewById(R.id.bt_write); Button btRead=(Button) this.findViewById(R.id.bt_read); btWrite.setOnClickListener(onClickListener); btRead.setOnClickListener(onClickListener); } /* 如果访问其他应用中的Preference,前提条件是:该preference创建时指定了Context.MODE_WORLD_READABLE或者Context.MODE_WORLD_WRITEABLE权限。如:有个<package name>为com.jbridge.pres.activity的应用使用下面语句创建了preference。 getSharedPreferences("zyj", Context.MODE_WORLD_READABLE); 其他应用要访问上面应用的preference,首先需要创建上面应用的Context,然后通过Context 访问preference ,访问preference时会在应用所在包下的shared_prefs目录找到preference : Context otherAppsContext = createPackageContext("com.jbridge.pres.activity", Context.CONTEXT_IGNORE_SECURITY); SharedPreferences sharedPreferences = otherAppsContext.getSharedPreferences("zyj", Context.MODE_WORLD_READABLE); String name = sharedPreferences.getString("name", "no name"); int age = sharedPreferences.getInt("age", 0); */ }
启动模拟器,运行程序。输入名称和年龄,点击保存。我们使用的代码是getSharedPreferences("zyj",Context.MODE_PRIVATE);,当然commit时。它会为我们为”/data/data/com.jbridge.pres.activity/shared_prefszyj.xml”。将 preferences.xml导出,查看它的内容为:
<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <map> <string name="name">zyj</string> <int name="age" value="22" /> </map>- 读写SharedPreferences中的数据
- 读写SharedPreferences中的数据
- 读写SharedPreferences中的数据
- 读写SharedPreferences/PreferenceActivity中的数据
- Android基础教程:读写SharedPreferences中的数据
- 使用SharedPreferences读写数据
- SharedPreferences读写数据实例
- 使用 SharedPreferences 读写数据
- 使用SharedPreferences读写数据
- Android之SharedPreferences读写数据
- 访问SharedPreferences中的数据
- Android中通过SharedPreferences读写数据
- Android-数据存储-SharedPreferences全局读写
- 读写配置文件中的数据
- android 读写其他应用SharedPreferences存储的数据
- 05 - 数据操作一:文件读写与XML解析、SharedPreferences
- 【Android】安卓学习笔记之使用SharedPreferences 读写数据
- Android 利用SharedPreferences进行数据存储和读写
- 使用DOM读取XML文件
- 《java并发编程实战》笔记(第8章)
- 使用Pull解析器读取XML文件和生成XML文件
- nova 冷迁移配置nova用户互信
- TCP、UDP、IP 协议分析
- 读写SharedPreferences中的数据
- 【Linux】中TCP的握手与挥手
- [DP优化] POJ 1160 Post Office
- Android之SQlite存储
- Android内容提供者(Content provider)
- TST运行报错解决
- java udp简单例子
- java tcp简单例子
- Linux_博客-端口分类调研