![[Android căn bản] Bài 11: Share Preferences trong Android](https://tuandc.com/wp-content/uploads/2017/08/Android.jpg)
[Android căn bản] Bài 11: Share Preferences trong Android

Chào các bạn chúng ta lại gặp nhau trong series Android truyền kì hôm nay mình sẽ giới thiệu đến các bạn về Share Preferences.
SharedPreferences là gì.
Trong Android có 5 cách lưu trữ dữ liệu là:
- Shared Preferences
- Internal Storage
- External Storage
- SQLite Database
- Network Connection
Share Preferences là một cách lưu trữ dữ liệu trong Android. Nó là một interface cho phép lưu trữ dữ liệu bằng cách cặp khá key và value và nó được lưu dưới dạng file xml trong chính ứng dụng Android luôn nếu bạn xóa app thì nó sẽ mất.
Và các dữ liệu lưu trong SharedPreferences là dạng dữ liệu nguyên thủy như int, float, string, boolean, long.
SharedPreferences chủ yếu sử dụng để lưu các dữ liệu đơn nhỏ lẻ hay các dữ liệu tạm như khi bạn chơi game thì bạn sẽ thấy có điểm số khi chơi và nó sẽ được lưu ở SharedPreferences.
Cách sử dụng SharedPreferences.
Cách sử dung nó cũng khá dễ.
Đầu tiên bạn thiết kế màn hình như sau.

Giao diện
Code:
<?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"
tools:context="com.tuandc.akinosora.sharepreferences.MainActivity">
<EditText
android:id="@+id/InputEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="92dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintHorizontal_bias="0.562"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Button
android:id="@+id/LuuButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lưu"
android:layout_marginTop="61dp"
app:layout_constraintTop_toBottomOf="@+id/InputEditText"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="0.532"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<TextView
android:id="@+id/OutputTextview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="34dp"
android:text="TextView"
app:layout_constraintHorizontal_bias="0.532"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/LuuButton"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Button
android:id="@+id/DocButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="43dp"
android:text="Đọc"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/OutputTextview"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
</android.support.constraint.ConstraintLayout>
Như trên hình ta thấy ta có một Edittext để nhập dữ liệu vào khi nhấn lưu thì sẽ lưu vô SharedPreferences . khi nhấn đọc nó sẽ đọc dữ liệu từ SharedPreferences
lên Textview.
Giờ ta sẽ viết code cho nó.
Code:
package com.tuandc.akinosora.sharepreferences;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button LưuButton, DocButton;
private EditText InputEditText;
private TextView OutputTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initControl();
initEvent();
}
private void initEvent() {
final SharedPreferences sharedPreferences = getSharedPreferences("Info", MODE_PRIVATE);// Khởi tạo share Preferences với tên share preferences và ché độ của share preferences.
LưuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SharedPreferences.Editor editor = sharedPreferences.edit();// Khởi tạo editor để chỉnh sửa SharedPreferences.
editor.putString("input", InputEditText.getText().toString());// Đẩy dữ liệu vào SharedPreferences.
editor.commit();/// Lưu SharedPreferences
InputEditText.setText("");
}
});
DocButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String Output = sharedPreferences.getString("input","");// Lấy dữ liệu từ SharedPreferences với hai tham số truyền vào là tên trường muốn lấy và giá trị mặc định nếu không có giá trị trong trường
OutputTextView.setText(Output);
}
});
}
private void initControl() {
LưuButton = (Button) findViewById(R.id.LuuButton);
DocButton = (Button) findViewById(R.id.DocButton);
InputEditText = (EditText) findViewById(R.id.InputEditText);
OutputTextView = (TextView) findViewById(R.id.OutputTextview);
}
}
Như vậy là xong bạn có thể chạy thử ứng dụng xem nó xem như thế nào.
Như vậy là tôi đã giới thiệu với các bạn cách sử dụng SharedPreferences
giờ các bạn có thể sử dụng nó để lưu các giá trị trong Android cảm ơn các bạn đã theo dõi và hẹn gặp lại các bạ n trong những bài tiếp theo của series Android truyền kì.
