Android--SharedPreference应用

    技术2025-03-26  13

    Java代码 注: 其它应用调用此参数文件:           //创建需要调用的参数保存文件应用的Context           //第一参数为保存文件所存在的包名           //第二个参数表示 忽略安全级别           //注: 这里的 this.getContext()是得到当前应用的上下文,因为本测试环境是继承 AndroidTestCase ,如果在Activity中可以直接使用createPackageContext方法           Context context = this.getContext().createPackageContext("cn.android.preference"this.getContext().CONTEXT_IGNORE_SECURITY);           //通过创建的context得到preferences对象           SharedPreferences preferences = context.getSharedPreferences("user", context.MODE_WORLD_READABLE);           //通过对象取得文件里面的值,也可以写入,这里就不再测试了           String name = preferences.getString("name""abc");           //将得到的值方在日志中,以便看程序是否正确执行           Log.e("TestSharedPreferenceParser",name);                      /** 需要注意的是:此文件在被其它应用读取的时候,其权限必须包含 context.MODE_WORLD_READABLE,否则无法找到  */    -------------------------------------------------------------------------------------------------------------------------------------》     package cn.android.preference;     import android.app.Activity;   import android.content.SharedPreferences;   import android.content.SharedPreferences.Editor;   import android.os.Bundle;   import android.view.View;   import android.widget.Button;   import android.widget.EditText;   import android.widget.Toast;     /**   * SharedPreferences实现参数化的数据保存与读取   * @author zengpeng    *   * 2011-1-30 下午02:35:44   */  public class PreferenceActivity extends Activity {         //定义一个文本输入框,用于得到输入的name值       private EditText nameText;       //定义一个文本输入框,用于得到输入的age值       private EditText ageText;                     @Override      public void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.main);                      //通过id得到id为name输入框           nameText = (EditText) findViewById(R.id.name);           //通过id得到id为age输入框           ageText = (EditText) findViewById(R.id.age);                      //通过id得到保存按钮           Button saveButton = (Button) findViewById(R.id.save);           //通过id得到读取按钮           Button readButton = (Button) findViewById(R.id.read);                      //为保存按钮添加事件           saveButton.setOnClickListener(new View.OnClickListener() {                 @Override              public void onClick(View v) {                   //通过Activity自带的getSharedPreferences方法,得到SharedPreferences对象                   //第一个参数表示保存后 xml 文件的名称(底层实现是将数据保存到xml文档中)。                   //第二个参数表示xml文档的权限为私有,并且重新写的数据会覆盖掉原来的数据                   SharedPreferences preferences = getSharedPreferences("user", PreferenceActivity.MODE_WORLD_READABLE);                                      //通过preferences得到它的编辑器对象edit                   Editor edit = preferences.edit();                   //通过编辑器将name属性和对应的nameText中输入的值写入到xml文档中                   edit.putString("name", nameText.getText().toString());                   //通过编辑器将age属性和对应的ageText中输入的值写入到xml文档中                   String ageStr = ageText.getText().toString();                   if(ageStr != null || "".equals(ageStr.trim()))                       edit.putInt("age"new Integer(ageStr));                   //添加数据完成后,提交编辑器的添加操作                   edit.commit();                   //提示用户保存成功                   Toast.makeText(PreferenceActivity.this, R.string.save_success, Toast.LENGTH_LONG).show();               }                          });                      //为读取按钮添加时间           readButton.setOnClickListener(new View.OnClickListener() {                 @Override              public void onClick(View v) {                   //通过Activity自带的getSharedPreferences方法,得到SharedPreferences对象                   //此时的第一个参数表示当前应用中的user.xml文件                   //如果只读的话,第二个参数没有什么意义,但方法参数需要,可以随便写                   SharedPreferences preferences = getSharedPreferences("user", PreferenceActivity.MODE_PRIVATE);                   //得到文件中的name标签值,第二个参数表示如果没有这个标签的话,返回的默认值                   String name = preferences.getString("name""");                   //得到文件中的age标签值,第二个参数表示如果没有这个标签的话,返回的默认值                   Integer age = preferences.getInt("age"0);                   //将读取的name值显示在文本编辑框nameText中                   nameText.setText(name);                   //将读取的age值显示在文本编辑框ageText中                   ageText.setText(age + "");                   //提示用户读取成功                   Toast.makeText(PreferenceActivity.this, R.string.read_success, Toast.LENGTH_LONG).show();               }                          });       }   }       ------------------------------------------------------------------>   页面布局mian.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">              <LinearLayout           android:orientation="horizontal"          android:layout_width="fill_parent"          android:layout_height="wrap_content">                  <TextView                 android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/name"/>                          <EditText               android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:id="@+id/name"  />             </LinearLayout>              <LinearLayout           android:orientation="horizontal"          android:layout_width="fill_parent"          android:layout_height="wrap_content">                  <TextView                 android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/age"/>                          <EditText               android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:id="@+id/age"   />             </LinearLayout>              <LinearLayout           android:orientation="horizontal"          android:layout_width="fill_parent"          android:layout_height="wrap_content">                      <Button               android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/save"                android:id="@+id/save" />                          <Button               android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="@string/read"                android:id="@+id/read" />             </LinearLayout>          </LinearLayout>  
    最新回复(0)