Giáo trình "Bài 17.2 - Xây dựng ứng dụng lưu trữ dữ liệu với share preferences"

Giáo trình "Bài 17.2 - Xây dựng ứng dụng lưu trữ dữ liệu với share preferences" gồm 3 trang giúp bạn ôn luyện và nắm vững kiến thức môn học. Mời bạn đọc đón xem!

BÀI 17_2. XÂY DNG NG DNG LƯU TRỮ D LIU VI SHARED
PREFERENCES
I. Mc tiêu
Giúp sinh viên hiu rõ và lp trình vi Shared Preferences để lưu trữ d liu.
II. Ni dung
1. Yêu cu: Viết ng dụng như sau:
Trong đó s dụng Shared Preferences để lưu lại lch s tính toán và hin th
trên TextView
Button Clear đa lch s
2. Các bưc thc hin:
c 1: To project mi và xây dng giao din activity_main.xml như sau:
Downloaded by Di?p DN - Chuyên Viên R&D (diepdn@bibabo.vn)
lOMoARcPSD|36212343
c 3: Code java trong MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText edta, edtb, edtkq;
Button btntong, btnclear;
TextView txtlichsu;
String lichsu = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edta = findViewById(R.id.edta);
edtb = findViewById(R.id.edtb);
edtkq = findViewById(R.id.edtkq);
btntong = findViewById(R.id.btntong);
btnclear = findViewById(R.id.btnclear);
txtlichsu = findViewById(R.id.txtlichsu);
//------Lấy lại dữ liệu trong SharedPreferences--------
SharedPreferences myprefs =
getSharedPreferences("mysave",MODE_PRIVATE);
lichsu = myprefs.getString("ls","");
txtlichsu.setText(lichsu);
//------------------------------------------
btntong.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int a = Integer.parseInt(edta.getText().toString());
int b = Integer.parseInt(edtb.getText().toString());
int kq = a + b;
edtkq.setText(kq+"");
lichsu += a+" + "+b+" = "+kq;
txtlichsu.setText(lichsu);
lichsu +="\n"; // Xuống dòng
}
});
btnclear.setOnClickListener(new View.OnClickListener() {
@Override
Downloaded by Di?p DN - Chuyên Viên R&D (diepdn@bibabo.vn)
lOMoARcPSD|36212343
public void onClick(View v) {
lichsu ="";
txtlichsu.setText(lichsu);
}
});
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences myprefs =
getSharedPreferences("mysave",MODE_PRIVATE);
SharedPreferences.Editor myedit = myprefs.edit();
myedit.putString("ls",lichsu);
myedit.commit();
}
}
---------------------END -----------------
Downloaded by Di?p DN - Chuyên Viên R&D (diepdn@bibabo.vn)
lOMoARcPSD|36212343
| 1/3

Preview text:

lOMoARcPSD|36212343
BÀI 17_2. XÂY DỰNG ỨNG DỤNG LƯU TRỮ DỮ LIỆU VỚI SHARED PREFERENCES I. Mục tiêu
Giúp sinh viên hiểu rõ và lập trình với Shared Preferences để lưu trữ dữ liệu. II. Nội dung
1. Yêu cầu
: Viết ứng dụng như sau:
Trong đó sử dụng Shared Preferences để lưu lại lịch sử tính toán và hiển thị trên TextView
Button Clear để xóa lịch sử
2. Các bước thực hiện:
Bước 1: Tạo project mới và xây dựng giao diện activity_main.xml như sau:
Downloaded by Di?p DN - Chuyên Viên R&D (diepdn@bibabo.vn) lOMoARcPSD|36212343
Bước 3: Code java trong MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText edta, edtb, edtkq;
Button btntong, btnclear; TextView txtlichsu;
String lichsu = ""; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edta = findViewById(R.id.edta);
edtb = findViewById(R.id.edtb);
edtkq = findViewById(R.id.edtkq);
btntong = findViewById(R.id.btntong);
btnclear = findViewById(R.id.btnclear);
txtlichsu = findViewById(R.id.txtlichsu);
//------Lấy lại dữ liệu trong SharedPreferences--------
SharedPreferences myprefs =
getSharedPreferences("mysave",MODE_PRIVATE);
lichsu = myprefs.getString("ls","");
txtlichsu.setText(lichsu);
//------------------------------------------
btntong.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
int a = Integer.parseInt(edta.getText().toString());
int b = Integer.parseInt(edtb.getText().toString()); int kq = a + b;
edtkq.setText(kq+"");
lichsu += a+" + "+b+" = "+kq;
txtlichsu.setText(lichsu);
lichsu +="\n"; // Xuống dòng } });
btnclear.setOnClickListener(new View.OnClickListener() { @Override
Downloaded by Di?p DN - Chuyên Viên R&D (diepdn@bibabo.vn) lOMoARcPSD|36212343
public void onClick(View v) { lichsu ="";
txtlichsu.setText(lichsu); } }); } @Override
protected void onPause() { super.onPause(); SharedPreferences myprefs =
getSharedPreferences("mysave",MODE_PRIVATE);
SharedPreferences.Editor myedit = myprefs.edit();
myedit.putString("ls",lichsu); myedit.commit(); } }
---------------------END -----------------
Downloaded by Di?p DN - Chuyên Viên R&D (diepdn@bibabo.vn)