Android提供了许多存储应用程序数据的方法.其中一种方法称为共享首选项.共享首选项允许您以键,值对的形式保存和检索数据.
为了使用共享首选项,您必须调用返回SharedPreference实例的方法getSharedPreferences()指向包含首选项值的文件.
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);
第一个参数是键,第二个参数是MODE.除了私人,还有其他可用的模式,列在下面和下面;
Sr.No | 模式&描述 |
---|---|
1 | MODE_APPEND 这将使用现有的首选项附加新的首选项 |
2 | MODE_ENABLE_WRITE_AHEAD_LOGGING 数据库打开标志.设置后,默认情况下会启用提前记录 |
3 | MODE_MULTI_PROCESS 即使已经加载了共享首选项实例,此方法也会检查首选项的修改 |
4 | MODE_PRIVATE 通过设置此模式,只能使用调用应用程序访问该文件 |
5 | MODE_WORLD_READABLE 此模式允许其他应用程序读取首选项 |
6 | MODE_WORLD_WRITEABLE 此模式允许其他应用程序编写首选项 |
您可以使用SharedPreferences.Editor类在共享首选项中保存一些内容.您将调用SharedPreference实例的edit方法,并将在编辑器对象中接收它.它的语法是 :
Editor editor = sharedpreferences.edit(); editor.putString("key", "value"); editor.commit();
除了putString方法之外,编辑器类中还有一些方法可以操作共享首选项中的数据.它们列出如下 :
Sr.否 | 模式&描述 |
---|---|
1 | apply() 这是一种抽象方法.它会将您的更改从编辑器提交回您正在调用的sharedPreference对象 |
2 | clear() 它将删除编辑器中的所有值 |
3 | remove(String key) 它将删除其键作为参数传递的值 |
4 | putLong(String key, long value) 它会在偏好编辑器中保存一个长值 |
5 | putInt(String key,int value) 它会在首选项编辑器中保存一个整数值 |
6 | putFloat(String key,float value) 它会在首选项编辑器中保存一个浮点值 |
此示例演示了共享的使用 喜好.它显示一个带有一些文本字段的屏幕,其值在应用程序关闭时保存,并在再次打开时返回.
要试验此示例,您需要在根据以下步骤开发应用程序后开启实际设备;
步骤 | 描述 |
---|---|
1 | 您将使用Android studio在com.example.sairamkrishna.myapplication包下创建一个Android应用程序. |
2 | 修改src/MainActivity.java文件以添加进度代码以显示旋转进度对话框. |
3 | 修改res/layout/activity_main.xml文件以添加相应的XML代码. |
4 | 运行应用程序并选择正在运行的Android设备并在其上安装应用程序并验证结果. |
以下是修改后的 MainActivity.java的内容.
package com.example.sairamkrishna.myapplication; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText ed1,ed2,ed3; Button b1; public static final String MyPREFERENCES = "MyPrefs" ; public static final String Name = "nameKey"; public static final String Phone = "phoneKey"; public static final String Email = "emailKey"; SharedPreferences sharedpreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ed1=(EditText)findViewById(R.id.editText); ed2=(EditText)findViewById(R.id.editText2); ed3=(EditText)findViewById(R.id.editText3); b1=(Button)findViewById(R.id.button); sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String n = ed1.getText().toString(); String ph = ed2.getText().toString(); String e = ed3.getText().toString(); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Name, n); editor.putString(Phone, ph); editor.putString(Email, e); editor.commit(); Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_LONG).show(); } }); } }
以下是修改后的主要活动文件 res/layout/activiy_main.xml.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Shared Preference " android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:textSize="35dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tutorials Point" android:id="@+id/textView2" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:textSize="35dp" android:textColor="#ff16ff01" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText" android:layout_below="@+id/textView2" android:layout_marginTop="67dp" android:hint="Name" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText2" android:layout_below="@+id/editText" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:hint="Pass" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/editText3" android:layout_below="@+id/editText2" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:hint="Email" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Save" android:id="@+id/button" android:layout_below="@+id/editText3" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" /> </RelativeLayout>
以下是文件 res/values/strings.xml的修改内容的内容.
<resources> <string name="app_name">My Application</string> </resources>
以下是内容默认文件 AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.sairamkrishna.myapplication" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们尝试运行您的应用程序.我假设您已将实际的Android移动设备与计算机相关联.要从Android工作室运行应用程序,请打开项目的一个活动文件,然后单击运行 icon从工具栏中.在开始申请之前,Android studio将显示以下窗口,以选择您要运行Android应用程序的选项.
选择您的移动设备作为选项,然后检查您的移动设备将显示以下屏幕和减号;
现在只需输入该领域的一些文字.就像我输入一些随机名称和其他信息并点击保存按钮.
现在按下保存按钮时,文本将保存在共享首选项中.现在按后退按钮退出应用程序.现在再打开它,你会看到你在应用程序中写回的所有文本.