博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android中SharedPreferences的简单例子
阅读量:4136 次
发布时间:2019-05-25

本文共 4032 字,大约阅读时间需要 13 分钟。

1:界面布局

[html] 
  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] 
  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] 
  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] 
  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] 
  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.     }  

转载地址:http://dclvi.baihongyu.com/

你可能感兴趣的文章
different aspects for software
查看>>
To do list
查看>>
Study of Source code
查看>>
如何使用BBC英语学习频道
查看>>
spring事务探索
查看>>
浅谈Spring声明式事务管理ThreadLocal和JDKProxy
查看>>
初识xsd
查看>>
java 设计模式-职责型模式
查看>>
构造型模式
查看>>
svn out of date 无法更新到最新版本
查看>>
java杂记
查看>>
RunTime.getRuntime().exec()
查看>>
Oracle 分组排序函数
查看>>
删除weblogic 域
查看>>
VMware Workstation 14中文破解版下载(附密钥)(笔记)
查看>>
日志框架学习
查看>>
日志框架学习2
查看>>
SVN-无法查看log,提示Want to go offline,时间显示1970问题,error主要是 url中 有一层的中文进行了2次encode
查看>>
NGINX
查看>>
Qt文件夹选择对话框
查看>>