Danh mục

Lập trình di động - Lab07: Data Storage

Số trang: 10      Loại file: pdf      Dung lượng: 1.05 MB      Lượt xem: 19      Lượt tải: 0    
tailieu_vip

Phí tải xuống: 3,000 VND Tải xuống file đầy đủ (10 trang) 0
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Nội dung của tài liệu trình bày về thao tác dữ liệu với SharedPreferences, thiết kế giao diện, xử lý code, gắn code cho các sự kiện, nơi lưu file, đọc ghi file Internal Storage – bộ nhớ trong, mô tả kịch bản, thiết kế màn hình, xử lý nút Read, xử lý nút Write, một số lưu ý và AutoComplete.
Nội dung trích xuất từ tài liệu:
Lập trình di động - Lab07: Data StorageVer 1.0 – 2016, FIT - HCMUPLab 07: Data Storage1 Thao tác dữ liệu với SharedPreferencesSharedPreferences dùng để lưu trạng thái của ứng dụng, dạng file xml. LớpSharedPreferences cung cấp một framework giúp bạn có thể lưu trữ và đọc lênnhững cặp key-value liên tục dữ liệu đơn giản. Có thể dùng SharedPreferences vớinhững kiểu dữ liệu như: booleans, floats, ints, longs, strings.1.1Thao tác đọc/ghi1.1.1 Ghi dữ liệu//Tạo đối tượng SharedPreferencesSharedPreferences sp = getSharedPreferences(fileName, MODE_PRIVATE);SharedPreferences.Editor editor = sp.edit();//Lưu dữ liệueditor.putX(key, value); //X là kiểu dữ liệu//Ví dụ kiểu stringeditor.putString(“ten”,”kylh”);//Hoàn thànheditor.commit();1.1.2 Đọc dữ liệu//Tạo đối tượng SharedPreferencesSharedPreferences sp = getSharedPreferences(fileName, MODE_PRIVATE);//Đọc dữ liệusp.getX(key, default); //X là kiểu dữ liệu//Ví dụ kiểu stringString t = sp.getString(“ten”, “”);Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM1Ver 1.0 – 2016, FIT - HCMUP1.2Thiết kế giao diện1.3Lab 07: Data StorageXử lý code đọc/ghiNế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. HCM2Ver 1.0 – 2016, FIT - HCMUPLab 07: Data Storageprivate 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à falseboolean bchk = pre.getBoolean(checked, false);if (bchk) {//lấy user, pwd, nếu không thấy giá trị mặc định là rỗngString 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 getSharedPreferencesSharedPreferences pre = this.getSharedPreferences(prefname,Context.MODE_PRIVATE);//tạo đối tượng Editor để lưu thay đổiSharedPreferences.Editor editor = pre.edit();//Lưu trữ dữ liệu dạng key/valueString 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. HCM3Ver 1.0 – 2016, FIT - HCMUPLab 07: Data Storageelse{//lưu vào editoreditor.putString(user, user);editor.putString(pwd, pwd);editor.putBoolean(checked, bchk);}//chấp nhận lưu xuống fileeditor.commit();}1.4Gắn code cho các sự kiện1.4.1 Xử lý cho LoginActivity@Overrideprotected 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() {@Overridepublic void onClick(View v) {//luu trang thaisavingPreferences();finish();//đóng màn hình hiện tạiIntent mh = new Intent(LoginActivity.this, LoginSuccessActivity.class);//truyền dữ liệu qua màn hình mớimh.putExtra(user, txtUser.getText().toString());startActivity(mh);//mở màn hình mới}});}@Overrideprotected void onPause() {Ths. Lương Trần Hy Hiến, KHOA CNTT – TRƯỜNG ĐH SƯ PHẠM TP. HCM4Ver 1.0 – 2016, FIT - HCMUPLab 07: Data Storagesuper.onPause();//gọi hàm lưu trạng tháisavingPreferences();}@Overrideprotected void onResume() {super.onResume();//gọi hàm đọc trạng thái ở đâyrestoringPreferences();}1.4.2 LoginSuccessActivityprotected 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() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubfinish();}});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. HCM5

Tài liệu được xem nhiều:

Gợi ý tài liệu liên quan: