การใช้การตั้งค่าร่วมกัน เราสามารถจัดเก็บหรือดึงค่าเป็นคู่ของคีย์และค่า มีห้าวิธีที่แตกต่างกันในการตั้งค่าการแชร์ดังที่แสดงด้านล่าง -
-
แก้ไข() − มันจะแก้ไขค่ากำหนดที่ใช้ร่วมกัน
-
กระทำ() - มันจะคอมมิตค่ากำหนดที่ใช้ร่วมกันในไฟล์ xml
-
สมัคร() − มันจะส่งกลับการเปลี่ยนแปลงจากตัวแก้ไขเป็นการตั้งค่าที่ใช้ร่วมกัน
-
ลบ(คีย์สตริง) − มันจะลบคีย์และวาลออกจากคีย์การใช้การตั้งค่าที่ใช้ร่วมกัน
-
ใส่() − มันจะใส่คีย์และค่าไปยัง xml การกำหนดลักษณะที่ใช้ร่วมกัน
ตัวอย่างไวยากรณ์ของการตั้งค่าที่ใช้ร่วมกันดังแสดงด้านล่าง -
final SharedPreferences sharedPreferences = getSharedPreferences("USER",MODE_PRIVATE); ในไวยากรณ์ข้างต้น เราได้สร้างไฟล์การกำหนดค่าตามความชอบที่ใช้ร่วมกันเป็น USER.xml และเป็นโหมดส่วนตัวหมายความว่าไม่มีแอปพลิเคชันอื่นที่สามารถเข้าถึงการตั้งค่าที่ใช้ร่วมกันนี้ได้
ตัวอย่างด้านล่างนี้สาธิตวิธีใช้การตั้งค่าที่ใช้ร่วมกันใน Android
ขั้นตอนที่ 1 − สร้างโครงการใหม่ใน Android Studio ไปที่ไฟล์ ⇒ โครงการใหม่และกรอกรายละเอียดที่จำเป็นทั้งหมดเพื่อสร้างโครงการใหม่
ขั้นตอนที่ 2 − เพิ่มรหัสต่อไปนี้ใน res/layout/activity_main.xml .
<?xml version = "1.0" encoding = "utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" android:orientation = "vertical" tools:context = ".MainActivity" tools:layout_editor_absoluteY = "81dp"> <EditText android:id = "@+id/name" android:layout_width = "match_parent" android:layout_height = "60dp" android:layout_marginTop = "8dp" android:autofillHints = "" android:hint = "NAME" app:layout_constraintTop_toTopOf = "parent" tools:layout_editor_absoluteX = "0dp" /> <EditText android:id = "@+id/address" android:layout_width = "match_parent" android:layout_height = "wrap_content" android:layout_marginTop = "84dp" android:hint = "Phone Number" android:importantForAutofill = "no" android:inputType = "" app:layout_constraintTop_toTopOf = "@+id/name" tools:layout_editor_absoluteX = "16dp" tools:targetApi = "o" /> <Button android:id = "@+id/button" android:layout_width = "108dp" android:layout_height = "wrap_content" android:layout_marginStart = "8dp" android:layout_marginLeft = "8dp" android:layout_marginTop = "120dp" android:layout_marginEnd = "8dp" android:layout_marginRight = "8dp" android:gravity = "center_horizontal" android:text = "Save" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintHorizontal_bias = "0.503" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toTopOf = "@+id/address" /> <Button android:id = "@+id/read" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_marginStart = "8dp" android:layout_marginLeft = "8dp" android:layout_marginTop = "88dp" android:layout_marginEnd = "8dp" android:layout_marginRight = "8dp" android:gravity = "center_horizontal" android:text = "read" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toBottomOf = "@+id/button" /> <TextView android:id = "@+id/result" android:layout_width = "wrap_content" android:layout_height = "0dp" android:layout_marginStart = "8dp" android:layout_marginLeft = "8dp" android:layout_marginTop = "184dp" android:layout_marginEnd = "8dp" android:layout_marginRight = "8dp" android:text = "result" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toBottomOf = "@+id/button" /> </android.support.constraint.ConstraintLayout>
ใน xml ด้านบน จะมีข้อความแก้ไขสองข้อความสำหรับชื่อและที่อยู่ เมื่อผู้ใช้คลิกปุ่มบันทึก ระบบจะเก็บค่าในการตั้งค่าที่ใช้ร่วมกัน และเมื่อผู้ใช้คลิกที่ปุ่มอ่าน ระบบจะอ่านค่าจากการตั้งค่าที่ใช้ร่วมกัน
ขั้นตอนที่ 3 − เพิ่มรหัสต่อไปนี้ใน src/MainActivity.java
package com.example.andy.myapplication;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final SharedPreferences sharedPreferences = getSharedPreferences("USER",MODE_PRIVATE);
final EditText name = findViewById(R.id.name);
final EditText address = findViewById(R.id.address);
final TextView result = findViewById(R.id.result);
Button save = findViewById(R.id.button);
Button read = findViewById(R.id.read);
read.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
result.setText("Name is "+sharedPreferences.getString("Name","No name")+" Address "+ sharedPreferences.getString("Address","No Address"));
}
});
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(name.getText().toString().isEmpty() && address.getText().toString().isEmpty()) {
Toast.makeText(MainActivity.this,"Plz Enter all the data",Toast.LENGTH_LONG).show();
} else {
String nameData = name.getText().toString().trim();
String addressData = address.getText().toString().trim();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("Name",nameData);
editor.putString("Address",addressData);
editor.commit();
}
}
});
}
}
ขั้นตอนที่ 4 − ไม่จำเป็นต้องเปลี่ยน manifest.xml มาลองเรียกใช้แอปพลิเคชันของคุณกัน ฉันคิดว่าคุณได้เชื่อมต่ออุปกรณ์มือถือ Android จริงกับคอมพิวเตอร์ของคุณ ในการรันแอพจาก android studio ให้เปิดไฟล์กิจกรรมของโปรเจ็กต์ของคุณแล้วคลิกไอคอน Run
จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณเป็นตัวเลือก แล้วตรวจสอบอุปกรณ์มือถือของคุณซึ่งจะแสดงหน้าจอเริ่มต้นของคุณ -

ในตัวอย่างข้างต้น เราได้เพิ่มชื่อและที่อยู่ และคลิกที่ปุ่มบันทึก

ในตัวอย่างข้างต้น เราได้คลิกที่ปุ่มอ่าน มันจะต่อท้ายข้อความเพื่อดูข้อความ