android中SharedPreferences的简单例子

1:界面布局

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="@string/name"  
  11.     />  
  12. <EditText  
  13.     android:id="@+id/name"  
  14.     android:layout_width="fill_parent"   
  15.     android:layout_height="wrap_content"   
  16.     />  
  17. <TextView    
  18.     android:layout_width="fill_parent"   
  19.     android:layout_height="wrap_content"   
  20.     android:text="@string/age"  
  21.     />  
  22. <EditText  
  23.     android:id="@+id/age"  
  24.     android:layout_width="fill_parent"   
  25.     android:layout_height="wrap_content"   
  26.     android:numeric="integer"  
  27.     />  
  28.   
  29. <LinearLayout   
  30.     android:orientation="horizontal"  
  31.     android:layout_width="fill_parent"  
  32.     android:layout_height="wrap_content"  
  33.     >  
  34. <Button  
  35.     android:id="@+id/button"  
  36.     android:layout_width="wrap_content"  
  37.     android:layout_height="wrap_content"  
  38.     android:text="@string/button"  
  39.    />  
  40. <Button  
  41.     android:id="@+id/resume"  
  42.     android:layout_width="wrap_content"  
  43.     android:layout_height="wrap_content"  
  44.     android:text="@string/resume"  
  45.    />  
  46. </LinearLayout>  
  47. </LinearLayout>  
2:用到的字符串资源strings.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World, MainActivity!</string>  
  4.     <string name="app_name">软件参数保存</string>  
  5.     <string name="name">网名</string>  
  6.     <string name="age">年龄</string>  
  7.     <string name="button">保存参数</string>  
  8.     <string name="success">参数保存成功</string>  
  9.     <string name="resume">恢复参数</string>  
  10. </resources>  

3:界面如下图所示

   程序界面如上图所示,用户在网名和年龄的文本框中输入相应的值,点击保存参数按钮,然后参数值就会保存到sharedpreferecs中,当两个EditText中没有值时,点击

恢复参数按钮会从sharedpreferences文件中读取name和age的值并显示在相应的控件上

4:MainActivity的内容

[html]  view plain copy
  1. package cn.itcast.sharedpreferences;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.SharedPreferences;  
  6. import android.content.SharedPreferences.Editor;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.Toast;  
  12.   
  13. public class MainActivity extends Activity {  
  14.     private EditText nameEditText = null;  
  15.     private EditText ageEditText = null;  
  16.     private Button saveButton = null;  
  17.     private Button resumeButton = null;  
  18.   
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.   
  24.         nameEditText = (EditText) findViewById(R.id.name);  
  25.         ageEditText = (EditText) findViewById(R.id.age);  
  26.         saveButton = (Button) findViewById(R.id.button);  
  27.         resumeButton = (Button) findViewById(R.id.resume);  
  28.   
  29.         saveButton.setOnClickListener(new View.OnClickListener() {  
  30.   
  31.             @Override  
  32.             public void onClick(View v) {  
  33.                 SharedPreferences sharedPreferences = getSharedPreferences("test", Context.MODE_WORLD_READABLE);  
  34.                 Editor editor = sharedPreferences.edit();  
  35.                 editor.putString("name", nameEditText.getText().toString());  
  36.                 editor.putInt("age", new Integer(ageEditText.getText()  
  37.                         .toString()));  
  38.                 editor.commit();  
  39.                 Toast.makeText(MainActivity.this, R.string.success, Toast.LENGTH_LONG)  
  40.                 .show();  
  41.             }  
  42.         });  
  43.           
  44.         resumeButton.setOnClickListener(new View.OnClickListener() {  
  45.               
  46.             @Override  
  47.             public void onClick(View v) {  
  48.                 SharedPreferences sharedPreferences = getSharedPreferences("test", Context.MODE_PRIVATE);  
  49.                 String name = sharedPreferences.getString("name", "");  
  50.                 int age = sharedPreferences.getInt("age", 20);  
  51.                 nameEditText.setText(name);  
  52.                 ageEditText.setText(String.valueOf(age));  
  53.             }  
  54.         });  
  55.     }  
  56. }  
5:生成的test.xml的内容为:

[html]  view plain copy
  1. <?xml version='1.0' encoding='utf-8' standalone='yes' ?>  
  2. <map>  
  3. <string name="name">zhangbo</string>  
  4. <int name="age" value="24" />  
  5. </map>  

6:注意当从外部程序对sharedprefences文件进行读取时,需要使用的程序为:

[java]  view plain copy
  1. public void testSharedPreference() {  
  2.         try {  
  3.             Context context = getContext().createPackageContext("cn.itcast.sharedpreferences",  
  4.                                                Context.CONTEXT_IGNORE_SECURITY);  
  5.             SharedPreferences sharedPreference = context.getSharedPreferences("test", Context.MODE_PRIVATE);  
  6.             String name = sharedPreference.getString("name""");  
  7.             int age = sharedPreference.getInt("age"0);  
  8.               
  9.             Log.i(TAG, "name:" + name + ", age:" + age);              
  10.         } catch (NameNotFoundException e) {  
  11.             e.printStackTrace();  
  12.         }  
  13.     }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值