ภาพรวม
ตัวอย่างนี้สาธิตวิธีการลบข้อมูลเฉพาะรายการ (Key) หรือลบทั้งหมดใน SharedPreferences
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project แล้วกรอกข้อมูลที่จำเป็น
ขั้นตอนที่ 2: จัดรูปแบบหน้าจอ (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="8dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete Shared Preference" />
<TextView
android:id="@+id/tvAfterChange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
ขั้นตอนที่ 3: กำหนด String Resources (res/values/strings.xml)
<resources>
<string name="app_name">SharedPref Demo</string>
<string name="sharedPref_key_player">player_name</string>
<string name="sharedPref_key_country">country_name</string>
</resources>
ขั้นตอนที่ 4: เขียนโค้ดหลัก (MainActivity.java)
package com.example.sharedprefdelete;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
TextView textView, tvAfterDelete;
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
tvAfterDelete = findViewById(R.id.tvAfterChange);
button = findViewById(R.id.button);
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
// บันทึกข้อมูลทดสอบ
editor.putString(getString(R.string.sharedPref_key_player), "Cristiano Ronaldo");
editor.putString(getString(R.string.sharedPref_key_country), "Portugal");
editor.apply();
// แสดงข้อมูลก่อนลบ
showData(textView, "SharedPreferences Values");
button.setOnClickListener(v -> {
// ลบข้อมูลเฉพาะ Key "country_name"
editor.remove(getString(R.string.sharedPref_key_country));
editor.apply();
// แสดงข้อมูลหลังลบ
showData(tvAfterDelete, "SharedPreferences Values - After Removing Country");
});
}
private void showData(TextView target, String title) {
String player = sharedPreferences.getString(getString(R.string.sharedPref_key_player), "");
String country = sharedPreferences.getString(getString(R.string.sharedPref_key_country), "");
target.setText(title + "\nPlayer: " + player + "\nCountry: " + country);
}
}
วิธีลบข้อมูลอื่นๆ
// ลบเฉพาะ Key ที่ระบุ
editor.remove("key_name");
editor.apply();
// ลบทุกข้อมูล (Clear all)
editor.clear();
editor.apply();
// หรือใช้ getSharedPreferences().edit().clear().apply(); โดยตรง
ขั้นตอนที่ 5: ตั้งค่า Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.sharedprefdelete">
<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>
การทดสอบ
รันแอปพลิเคชันบนอุปกรณ์จริง กดปุ่ม "Delete Shared Preference" จะลบข้อมูล Country ออก ทำให้แสดงแค่ Player เหลืออยู่
