如何在Android中使用SharedPreferences存储、读取和编辑值?
本示例演示了如何在Android中使用SharedPreferences存储、读取和编辑值。
步骤1 - 在Android Studio中创建一个新项目,转到文件 ⇒ 新建项目,并填写所有必需的详细信息以创建新项目。
步骤2 - 将以下代码添加到res/layout/activity_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:padding="8dp" tools:context=".MainActivity"> <Button android:id="@+id/btnSave" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_centerInParent="true" android:onClick="Save" android:text="Save" /> <Button android:id="@+id/btnRetrieve" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_centerHorizontal="true" android:onClick="Get" android:text="Read" /> <Button android:id="@+id/btnClear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentEnd="true" android:layout_centerInParent="true" android:onClick="clear" android:text="Clear" /> <EditText android:id="@+id/etEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/etName" android:layout_alignParentEnd="true" android:layout_marginTop="10dp" 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:layout_alignStart="@+id/etEmail" android:layout_marginTop="40dp" android:ems="10" android:hint="Name" android:inputType="text" /> </RelativeLayout>
步骤3 - 将以下代码添加到src/MainActivity.java
package app.com.sample; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText editTextName, editTextEmail; public static final String mypreference = "mypref"; public static final String Name = "nameKey"; public static final String Email = "emailKey"; SharedPreferences sharedpreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editTextEmail = findViewById(R.id.etEmail); editTextName = findViewById(R.id.etName); sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE); if (sharedpreferences.contains(Name)) { editTextName.setText(sharedpreferences.getString(Name, "")); } if (sharedpreferences.contains(Email)) { editTextEmail.setText(sharedpreferences.getString(Email, "")); } } public void Save(View view) { String strName = editTextName.getText().toString(); String strEmail = editTextEmail.getText().toString(); SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(Name, strName); editor.putString(Email, strEmail); editor.apply(); Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show(); } public void clear(View view) { editTextName = findViewById(R.id.etName); editTextEmail = findViewById(R.id.etEmail); editTextName.setText(""); editTextEmail.setText(""); Toast.makeText(getApplicationContext(), "Cleared", Toast.LENGTH_SHORT).show(); } public void Get(View view) { editTextName = findViewById(R.id.etName); editTextEmail = findViewById(R.id.etEmail); sharedpreferences = getSharedPreferences(mypreference, Context.MODE_PRIVATE); if (sharedpreferences.contains(Name)) { editTextName.setText(sharedpreferences.getString(Name, "")); } if (sharedpreferences.contains(Email)) { editTextEmail.setText(sharedpreferences.getString(Email, "")); } Toast.makeText(getApplicationContext(), "Retrieved", Toast.LENGTH_SHORT).show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); return true; } }
步骤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运行应用程序,请打开您项目中的一个活动文件,然后单击运行 工具栏中的图标。选择您的移动设备作为选项,然后检查您的移动设备,它将显示您的默认屏幕 -
点击此处下载项目代码。
广告