如何在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執行應用程式,請開啟您專案中的一個活動檔案,然後單擊執行 工具欄中的圖示。選擇您的移動裝置作為選項,然後檢查您的移動裝置,它將顯示您的預設螢幕 -
點選此處下載專案程式碼。
廣告