1. 程式人生 > >使用者登入記住密碼--SharedPreferences應用

使用者登入記住密碼--SharedPreferences應用

1。介面程式碼

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/logintop_roundbg"
android:padding="@dimen/activity_horizontal_margin">

    <EditText
android:id="@+id/etName" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/icon_user" android:drawablePadding="10dp" android:ems="10" android:hint="@string/etName" /> <requestFocus /> <EditText
android:id="@+id/etPass" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/etName" android:background="@android:drawable/edit_text" android:drawableLeft="@drawable/icon_pass" android:drawablePadding="10dp" android:ems="10" android:hint="@string/etPassword" android
:inputType="textPassword" /> <requestFocus /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/etPass"> <CheckBox android:text="記住密碼" android:layout_width="0dp" android:layout_height="wrap_content" android:id="@+id/rememberpass" android:layout_weight="1" android:textSize="20sp"/> <Button android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/btn_select" android:text="@string/btnLogin" android:onClick="login"/> </LinearLayout> </RelativeLayout>
2.實現登入程式碼
package cn.edu.bzu.case_login;

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.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

public class Login_Activity extends AppCompatActivity {
    private EditText etname;
    private EditText etpass;
    private SharedPreferences sharedPreferences;
    private CheckBox rememberpass;

    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initviews();
        sharedPreferences = getSharedPreferences("remember", Context.MODE_PRIVATE);
         boolean remember=sharedPreferences.getBoolean("remember",false);
        if (remember){
            String name=sharedPreferences.getString("name","");
            String pass=sharedPreferences.getString("pass","");
            etname.setText(name);
            etpass.setText(pass);
            rememberpass.setChecked(true);
        }

    }

    private void initviews() {
        etname = (EditText) findViewById(R.id.etName);
        etpass = (EditText) findViewById(R.id.etPass);
        rememberpass = (CheckBox) findViewById(R.id.rememberpass);
    }

    public void login(View view) {
        String name = etname.getText().toString();
        String pass = etpass.getText().toString();
        if ("admin".equals(name) && "123456".equals(pass)) {
            SharedPreferences.Editor editor = sharedPreferences.edit();

        if (rememberpass.isChecked()) {
            editor.putBoolean("remember",true);
            editor.putString("name",name);
            editor.putString("pass",pass);
        }else{
            editor.clear();
        }
            editor.commit();
            Intent intent=new Intent(this,MainActivity.class);
            startActivity(intent);
            finish();
    }else {
            Toast.makeText(this,"賬號或密碼錯誤",Toast.LENGTH_LONG).show();
        }
}
3.登入跳轉頁面
package cn.edu.bzu.case_login;

import android.app.Activity;
import android.os.Bundle;

/**
 * Created by ASUS on 2017/4/8.
 */
public class MainActivity extends Activity{
    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}