1. Trang chủ
  2. » Công Nghệ Thông Tin

Android Programing Bài 9: Asset (part 2)

11 102 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 11
Dung lượng 237,45 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Lưu trạng thái của ứng dụng Bước 1: – Gọi hàm getSharedPreferences, hàm này trả về SharedPreferences và nó có 2 đối.. Chú ý là đối số 1 ta chỉ ghi tên tập tin không cần ghi phần mở rộng,

Trang 1

BÀI 9: Asset - Shared preference - Memory Device

 (Bài tập ứng dụng)

I Lưu trạng thái của ứng dụng

Bước 1:

– Gọi hàm getSharedPreferences, hàm này trả về SharedPreferences và nó có 2 đối Đối số 1 là tên tập tin để lưu trạng thái, đối số 2 là kiểu lưu Chú ý là đối

số 1 ta chỉ ghi tên tập tin (không cần ghi phần mở rộng, vì phần mở rộng mặc nhiên của nó là .xml khi ta lưu thành công), đối số 2 thường ta để là MODE_PRIVATE:

SharedPreferences pre=getSharedPreferences(“my_data“,

MODE_PRIVATE);

Bước 2:

– Tạo đổi tượng Editor để cho phép chỉnh sửa dữ liệu:

SharedPreferences.Editor edit=pre.edit();

Bước 3:

– Đưa dữ liệu muốn lưu trữ vào edit bằng các phương thức edit.putXXX(“key”,”value”);

Tùy vào kiểu dữ liệu ta muốn lưu trữ mà XXX được thay thế bởi các kiểu dữ liệu phù hợp:

editor.putString(“user”, “drthanh”);

editor.putString(“pwd”, “hoilamgi”);

editor.putBoolean(“checked”, true);

Bước 4:

– Lưu trạng thái bằng cách gọi dòng lệnh: editor.commit();

Sau khi bạn làm đúng 4 bước trên thì chương trình sẽ lưu được trạng thái, như trên

đã nói mặc định phần mở rộng là xml (tức là trạng thái được lưu dưới định dạng

tập tin XML) Bên trên ta đặt tên là my_data có nghĩa là chương trình sẽ tạo ra tập tin my_data.xml – tự động nó sẽ lưu vào thư mục shared_prefs như hình bên

Trang 2

II Cách đọc trạng thái lưu

Bước 1:

– Gọi hàm getSharedPreferences để trả về đối tượng SharedPreferences (giống như phần lưu trạng thái mà Tôi nói ở trên)

SharedPreferences pre=getSharedPreferences

(“my_data“,MODE_PRIVATE);

Bước 2:

– Gọi các phương thức getXXX(“key”,giá trị mặc định) để lấy các giá trị lúc trước được lưu

boolean bchk=pre.getBoolean(“checked”, false);//đối số 2 Tôi để false là giá

trị mặc định khi nó tìm không thấy key =checked

String user=pre.getString(“user”, “”);//lấy giá trị được lưu trong key=user,

nếu không thấy thì gán giá trị mặc định là chuỗi rỗng

String pwd=pre.getString(“pwd”, “”);//giống trên

Ví dụ:tạo một màn hình đăng nhập có checkbox cho phép lưu lại thông tin đăng nhập, lần sau khởi đội lại thì nó sẽ lấy lại thông tin nhập lúc trước để

Trang 3

người sử dụng đỡ phải mất công nhập lại (biết về Shared Preferences), xem hình mình họa:

 Khi chọn nút đăng nhập, chương trình sẽ đóng Activity hiện tại và hiển thị Activity dưới đây:

 Chọn nút Thoát, chương trình tiếp tục đóng Activity này giúp tắt hẳn các Activity trong ứng dụng

 Khởi động lại chương trình sẽ phải tự động load lại thông tin đăng nhập trước

đó (Nếu khi đăng nhập có chọn “Lưu thông tin“).

 Cấu trúc của Project:

Trang 4

– Layout XML của màn hình chính (activity_main.xml):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/LinearLayout1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity" >

<TextView

android:id="@+id/textView1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="#80FFFF"

android:gravity="center"

android:text="Đăng nhập hệ thống" />

Trang 5

android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="*"

>

<TableRow

android:id="@+id/tableRow1"

android:layout_width="wrap_content" android:layout_height="wrap_content" >

<TextView

android:id="@+id/textView2"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="User:" />

<EditText

android:id="@+id/editUser"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="text"

android:ems="10" >

<requestFocus />

</EditText>

</TableRow>

<TableRow

android:id="@+id/tableRow2"

android:layout_width="wrap_content" android:layout_height="wrap_content" > </TableRow>

<TableRow

android:id="@+id/tableRow3"

android:layout_width="wrap_content" android:layout_height="wrap_content" >

Trang 6

android:id="@+id/textView3"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pasword:" />

<EditText

android:id="@+id/editPassword"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10"

android:inputType="textPassword" /> </TableRow>

<TableRow

android:id="@+id/tableRow4"

android:layout_width="wrap_content" android:layout_height="wrap_content" >

<CheckBox

android:id="@+id/chksaveacount"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1"

android:text="Lưu thông tin" />

</TableRow>

<TableRow

android:id="@+id/tableRow5"

android:layout_width="wrap_content" android:layout_height="wrap_content" >

<Button

android:id="@+id/btnlogin"

android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_column="1"

android:text="Đăng nhập" />

Trang 7

</TableRow>

</TableLayout>

</LinearLayout>

– Layout XML của activity_dang_nhap_thanh_cong.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/LinearLayout1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".DangNhapThanhCongActivity" >

<TextView

android:id="@+id/txtmsg"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="TextView"

android:textSize="20sp" />

<Button

android:id="@+id/btnThoat"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:text="Thoát" />

</LinearLayout>

– Bạn xem Source code MainActivity.java:

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.content.SharedPreferences;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.EditText;

Trang 8

public class MainActivity extends Activity {

Button btnlogin;

EditText edituser,editpassword;

CheckBox chksaveaccount;

//đặt tên cho tập tin lưu trạng thái

String prefname="my_data";

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

btnlogin=(Button) findViewById(R.id.btnlogin);

edituser =(EditText)

findViewById(R.id.editUser);

editpassword=(EditText)

findViewById(R.id.editPassword);

chksaveaccount=(CheckBox)

findViewById(R.id.chksaveacount);

btnlogin.setOnClickListener(

new View.OnClickListener() {

public void onClick(View arg0) {

doLogin();

}

});

}

/**

* hàm đăng nhập hệ thống

*/

public void doLogin()

{

finish();//đóng màn hình hiện tại

Intent i=new Intent(this, DangNhapThanhCongActivity.class); //truyền dữ liệu qua màn hình mới

i.putExtra("user", edituser.getText().toString());

startActivity(i);//mở màn hình mới

}

@Override

protected void onPause() {

// TODO Auto-generated method stub

super.onPause();

//gọi hàm lưu trạng thái ở đây

savingPreferences();

Trang 9

}

@Override

protected void onResume() {

// TODO Auto-generated method stub

super.onResume();

//gọi hàm đọc trạng thái ở đây

restoringPreferences();

}

/**

* hàm lưu trạng thái

*/

public void savingPreferences()

{

//tạo đối tượng getSharedPreferences

SharedPreferences pre=getSharedPreferences (prefname, MODE_PRIVATE);

//tạo đối tượng Editor để lưu thay đổi

SharedPreferences.Editor editor=pre.edit(); String user=edituser.getText().toString(); String pwd=editpassword.getText().toString(); boolean bchk=chksaveaccount.isChecked(); if(!bchk)

{

//xóa mọi lưu trữ trước đó

editor.clear();

}

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();

}

/**

* hàm đọc trạng thái đã lưu trước đó

*/

public void restoringPreferences()

Trang 10

{

SharedPreferences pre=getSharedPreferences

(prefname,MODE_PRIVATE);

//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", "");

String pwd=pre.getString("pwd", "");

edituser.setText(user);

editpassword.setText(pwd);

}

chksaveaccount.setChecked(bchk);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present getMenuInflater().inflate(R.menu.activity_main, menu);

return true;

}

}

Bạn xem source code DangNhapThanhCongActivity.java:

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.view.Menu;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.TextView;

public class DangNhapThanhCongActivity extends Activity {

TextView txtMsg;

Button btnThoat;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_dang_nhap_thanh_cong);

Trang 11

txtMsg=(TextView) findViewById(R.id.txtmsg);

btnThoat=(Button) findViewById(R.id.btnThoat);

btnThoat.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

// TODO Auto-generated method stub

finish();

}

});

Intent i=getIntent();

txtMsg.setText("Hello : "+i.getStringExtra("user"));

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present

getMenuInflater().inflate(R.menu.activity_dang_nhap_thanh_cong, menu); return true;

}

}

Ngày đăng: 22/07/2019, 14:56

TỪ KHÓA LIÊN QUAN

TÀI LIỆU CÙNG NGƯỜI DÙNG

  • Đang cập nhật ...

TÀI LIỆU LIÊN QUAN

w