如何在 Android 中检索其他应用程序的共享的首选项?
本示例演示如何在 Android 中执行 I。
步骤 1 − 在 Android Studio 中创建一个新项目,转到文件 ⇒ 新项目,然后填写所有必需的详细信息以创建一个新项目。
步骤 2 − 将以下代码添加到 res/layout/activity_main.xml。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:padding="16dp" tools:context=".MainActivity"> <EditText android:id="@+id/etEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:ems="10" android:hint="Email" android:inputType="textEmailAddress" /> <EditText android:id="@+id/etName" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="Name" android:inputType="text" /> <Button android:layout_marginTop="50dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="Save" android:text="Save" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="Clear" android:text="Clear Data" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="Retrieve" android:text="Retrieve Data" /> </LinearLayout>
步骤 3 − 将以下代码添加到 src/MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText etName, etEmail;
SharedPreferences sharedPreferences;
public static final String myPreference = "myPref";
public static final String Name = "nameKey";
public static final String Email = "emailKey";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = findViewById(R.id.etName);
etEmail = findViewById(R.id.etEmail);
sharedPreferences = getSharedPreferences(myPreference, Context.MODE_PRIVATE);
if (sharedPreferences.contains(Name)) {
etName.setText(sharedPreferences.getString(Name, ""));
}
if (sharedPreferences.contains(Email)) {
etEmail.setText(sharedPreferences.getString(Email, ""));
}
}
public void Clear(View view) {
etName = findViewById(R.id.etName);
etEmail = findViewById(R.id.etEmail);
etName.setText("");
etEmail.setText("");
Toast.makeText(MainActivity.this, "Cleared", Toast.LENGTH_LONG).show();
}
public void Retrieve(View view) {
etName = findViewById(R.id.etName);
etEmail = findViewById(R.id.etEmail);
sharedPreferences = getSharedPreferences(myPreference, Context.MODE_PRIVATE);
if (sharedPreferences.contains(Name)) {
etName.setText(sharedPreferences.getString(Name, ""));
}
if (sharedPreferences.contains(Email)) {
etEmail.setText(sharedPreferences.getString(Email, ""));
}
Toast.makeText(MainActivity.this, "Retrieved", Toast.LENGTH_LONG).show();
}
public void Save(View view) {
String name = etName.getText().toString();
String email = etEmail.getText().toString();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(Name, name);
editor.putString(Email, email);
editor.apply();
Toast.makeText(MainActivity.this, "Saved", Toast.LENGTH_LONG).show();
}
}步骤 4 − 将以下代码添加到 androidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.com.sample"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们尝试运行您的应用程序。我假设您已将您的实际 Android 移动设备连接到您的计算机。要从 Android Studio 运行应用程序,请打开一个项目的活动文件并单击运行
工具栏中的图标。选择您的移动设备作为选项,然后检查会显示您默认屏幕的移动设备 −


单击此处 下载 项目代码。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP