Android - 共享首选项
Android 提供了多种方式来存储应用程序的数据。 其中一种方式称为共享首选项。 共享首选项允许您以键值对的形式保存和检索数据。
为了使用共享首选项,您必须调用 getSharedPreferences() 方法,该方法返回指向包含首选项值的文件的 SharedPreference 实例。
SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
第一个参数是key,第二个参数是MODE。 除了私人之外,还有其他可用的模式,如下所示 −
| 序号 | 模式 & 描述 |
|---|---|
| 1 |
MODE_APPEND 这会将新的首选项附加到现有的首选项中 |
| 2 |
MODE_ENABLE_WRITE_AHEAD_LOGGING 数据库打开标志。 设置后,默认启用预写日志记录 |
| 3 |
MODE_MULTI_PROCESS 即使已加载 sharedpreference 实例,此方法也会检查首选项的修改 |
| 4 |
MODE_PRIVATE 通过设置此模式,文件只能使用调用应用程序访问 |
| 5 |
MODE_WORLD_READABLE 此模式允许其他应用程序读取首选项 |
| 6 |
MODE_WORLD_WRITEABLE 此模式允许其他应用程序编写首选项 |
您可以使用 SharedPreferences.Editor 类将某些内容保存在 sharedpreferences 中。 您将调用 SharedPreference 实例的编辑方法,并将在编辑器对象中接收它。 它的语法是 −
Editor editor = sharedpreferences.edit();
editor.putString("key", "value");
editor.commit();
除了 putString 方法之外,编辑器类中还有一些方法允许在共享首选项中操作数据。 它们列出如下 −
| Sr. NO | Mode & 描述 |
|---|---|
| 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>
Following is the content default file 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 Studio 运行应用程序,请打开项目的活动文件之一,然后单击工具栏中的 Run
图标。在启动您的应用程序之前,Android Studio 将显示以下窗口以选择您要运行 Android 应用程序的选项。
选择您的移动设备作为选项,然后检查您的移动设备,它将显示以下屏幕 −
现在只需在该字段中输入一些文本。 就像我放了一些随机名称和其他信息,然后单击保存按钮。
现在,当您按下保存按钮时,文本将保存在共享首选项中。 现在按返回按钮并退出应用程序。 现在再次打开它,您将看到您在应用程序中写回的所有文本。

