Giới thiệu
Hôm nay chúng ta sẽ tạo một ứng dụng. Khi chạy dứng ụng nó sẽ hiện màng hình đăng nhập. Người dùng nhập username và password vào, nếu đến sẽ chuyển đến màng hình khác, thông báo đăng nhập thành công.Ở màng hình đăng nhập có 1 checkbox, nếu nó được check lần sau, khi mở ứng dụng...nó sẽ load lại username và password đã đăng nhập lần trước.
Android cung cấp rất nhiều các để lưu trữ dữ liệu. Một trong số đó gọi là Shared Preferences, nó cho phép lưu và truy xuất dữ liệu thông qua cặp Key,Value.
Màng hình đăng nhập:
activity_main.xml<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edtUser" android:layout_marginTop="67dp" android:hint="Username" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/edtPass" android:layout_below="@+id/edtUser" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:inputType="textPassword" android:hint="Password" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Đăng nhập" android:id="@+id/btnLogin" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:layout_below="@+id/edtPass" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Ghi nhớ" android:id="@+id/cbRemember" android:layout_below="@+id/edtPass" android:layout_toRightOf="@+id/btnLogin" android:layout_toEndOf="@+id/btnLogin" android:checked="false" /> </RelativeLayout>MainActivity.java
package com.androidtmc.sharedpreferencesexample; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends AppCompatActivity { EditText edtUserName, edtPass; Button btnLogin; CheckBox cbRemember; public static final String MyPREFERENCES = "MyPrefs"; public static final String USERNAME = "userNameKey"; public static final String PASS = "passKey"; public static final String REMEMBER = "remember"; SharedPreferences sharedpreferences; String userLogin = "androidTMC"; String passLogin = "androidtmc"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //khởi tạo shared preferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); initWidgets();//khởi tạo các control loadData();//lấy dữ liệu đã lưu nếu có //thiết đặt button đăng nhập btnLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //nếu người dùng có chọn ghi nhớ if (cbRemember.isChecked()) //lưu lại thông tin đăng nhập saveData(edtUserName.getText().toString(), edtPass.getText().toString()); else clearData();//xóa thông tin đã lưu //nếu thông tin đăng nhập đúng thì đến màng hình home if (edtUserName.getText().toString().equals(userLogin) && edtPass.getText().toString().equals(passLogin)) { Intent intent = new Intent(MainActivity.this, HomeActivity.class); startActivity(intent); } else Toast.makeText(MainActivity.this, "Thông tin đăng nhập không đúng", Toast.LENGTH_SHORT).show(); } }); } private void clearData() { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.clear(); editor.commit(); } private void saveData(String username, String Pass) { SharedPreferences.Editor editor = sharedpreferences.edit(); editor.putString(USERNAME, username); editor.putString(PASS, Pass); editor.putBoolean(REMEMBER,cbRemember.isChecked()); editor.commit(); } private void loadData() { if(sharedpreferences.getBoolean(REMEMBER,false)) { edtUserName.setText(sharedpreferences.getString(USERNAME, "")); edtPass.setText(sharedpreferences.getString(PASS, "")); cbRemember.setChecked(true); } else cbRemember.setChecked(false); } private void initWidgets() { edtUserName = (EditText) findViewById(R.id.edtUser); edtPass = (EditText) findViewById(R.id.edtPass); cbRemember = (CheckBox) findViewById(R.id.cbRemember); btnLogin = (Button) findViewById(R.id.btnLogin); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Màng hình Home (đăng nhập thành công)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.androidtmc.sharedpreferencesexample.HomeActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Đăng nhập thành công" /> </RelativeLayout>
Chạy ứng dụng để xem kết quả!
Source code


0 nhận xét:
Đăng nhận xét