Xem mẫu

Ver 1.0 – 2016, FIT - HCMUP

Lab 07: Data Storage

1 Thao tác dữ liệu với SharedPreferences
SharedPreferences dùng để lưu trạng thái của ứng dụng, dạng file xml. Lớp
SharedPreferences cung cấp một framework giúp bạn có thể lưu trữ và đọc lên
những cặp key-value liên tục dữ liệu đơn giản. Có thể dùng SharedPreferences với
những kiểu dữ liệu như: booleans, floats, ints, longs, strings.
1.1

Thao tác đọc/ghi

1.1.1 Ghi dữ liệu

//Tạo đối tượng SharedPreferences
SharedPreferences sp = getSharedPreferences(fileName, MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
//Lưu dữ liệu
editor.putX(key, value); //X là kiểu dữ liệu
//Ví dụ kiểu string
editor.putString(“ten”,”kylh”);
//Hoàn thành
editor.commit();
1.1.2 Đọc dữ liệu

//Tạo đối tượng SharedPreferences
SharedPreferences sp = getSharedPreferences(fileName, MODE_PRIVATE);
//Đọc dữ liệu
sp.getX(key, default); //X là kiểu dữ liệu
//Ví dụ kiểu string
String t = sp.getString(“ten”, “”);

Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM

1

Ver 1.0 – 2016, FIT - HCMUP

1.2

Thiết kế giao diện

1.3

Lab 07: Data Storage

Xử lý code đọc/ghi

Nếu người dùng check chọn ghi nhớ:

Tiến hành kiểm tra đăng nhập thành công (nếu có), lưu thông tin và mở màn hình
(Activity) cá nhân.

Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM

2

Ver 1.0 – 2016, FIT - HCMUP

Lab 07: Data Storage

private void restoringPreferences() {
SharedPreferences pre = this.getSharedPreferences(prefname,
Context.MODE_PRIVATE);
if(pre != null) {
//lấy giá trị checked ra, nếu không thấy thì giá trị mặc định là false
boolean bchk = pre.getBoolean("checked", false);
if (bchk) {
//lấy user, pwd, nếu không thấy giá trị mặc định là rỗng
String user = pre.getString("user", "admin");
String pwd = pre.getString("pwd", "123");
txtUser.setText(user);
txtPass.setText(pwd);
}
chkGhiNho.setChecked(bchk);
}
}
private void savingPreferences(){
//tạo đối tượng getSharedPreferences
SharedPreferences pre = this.getSharedPreferences(prefname,
Context.MODE_PRIVATE);
//tạo đối tượng Editor để lưu thay đổi
SharedPreferences.Editor editor = pre.edit();
//Lưu trữ dữ liệu dạng key/value
String user = txtUser.getText().toString();
String pwd = txtPass.getText().toString();
boolean bchk = chkGhiNho.isChecked();
if(!bchk)
{
//xóa mọi lưu trữ trước đó
editor.clear();
}
Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM

3

Ver 1.0 – 2016, FIT - HCMUP

Lab 07: Data Storage

else
{
//lưu vào editor
editor.putString("user", user);
editor.putString("pwd", pwd);
editor.putBoolean("checked", bchk);
}
//chấp nhận lưu xuống file
editor.commit();
}
1.4

Gắn code cho các sự kiện

1.4.1 Xử lý cho LoginActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnDangNhap = (Button)findViewById(R.id.btnDangNhap);
txtUser = (TextView)findViewById(R.id.txtUser);
txtPass = (TextView)findViewById(R.id.txtPass);
chkGhiNho = (CheckBox)findViewById(R.id.chkGhiNho);
btnDangNhap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//luu trang thai
savingPreferences();
finish();//đóng màn hình hiện tại
Intent mh = new Intent(LoginActivity.this, LoginSuccessActivity.class);
//truyền dữ liệu qua màn hình mới
mh.putExtra("user", txtUser.getText().toString());
startActivity(mh);//mở màn hình mới
}
});
}

@Override
protected void onPause() {
Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM

4

Ver 1.0 – 2016, FIT - HCMUP

Lab 07: Data Storage

super.onPause();
//gọi hàm lưu trạng thái
savingPreferences();
}
@Override
protected void onResume() {
super.onResume();
//gọi hàm đọc trạng thái ở đây
restoringPreferences();
}
1.4.2 LoginSuccessActivity

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_success);
txtMsg=(TextView) findViewById(R.id.txtmsg);
btnThoat=(Button) findViewById(R.id.btnThoat);
btnThoat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
finish();
}
});
Intent i = getIntent();
txtMsg.setText("Hello : " + i.getStringExtra("user"));
}

Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM

5

nguon tai.lieu . vn