SharedPreferences记住登录名、密码

package com.example.sharedpreferencestest;

import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    private EditText mEditTextUsername;
    private EditText mEditTextPassword;
    private Button login;
    private CheckBox mRememberPass;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /**
         * 调用PreferenceManager的getDefaultSharedPreferences()方法,
         * 这是静态方法,接受一个Context参数,自动使用当前应用程序的包名为前缀命名SharedPreferences文件
         */
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        mEditTextUsername = (EditText) findViewById(R.id.edit_username);
        mEditTextPassword = (EditText) findViewById(R.id.edit_password);
        login = (Button) findViewById(R.id.button_login);
        mRememberPass = (CheckBox) findViewById(R.id.remember_password);

        boolean isRemember = pref.getBoolean("remember_password", false);
        String username = pref.getString("username", "");
        mEditTextUsername.setText(username);
        if(isRemember){
            //将账号和密码设置到文本框中

            String password = pref.getString("password", "");

            mEditTextPassword.setText(password);
            mRememberPass.setChecked(true);
        }
        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String username = mEditTextUsername.getText().toString();
                String password = mEditTextPassword.getText().toString();
                if(username.equals("Tom")&&password.equals("123456")){
                    editor = pref.edit();
                    editor.putString("username", username);
                    if(mRememberPass.isChecked()){
                        editor.putBoolean("remember_password", true);

                        editor.putString("password", password);
                    }else{
                        editor.clear();
                    }
                    editor.commit();
                    Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                    startActivity(intent);
                    finish();
                }
                else{
                    Toast.makeText(MainActivity.this, "登录名或密码错误", Toast.LENGTH_LONG).show();
                }
            }
        });
    }

}

布局文件

<LinearLayout 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:orientation="vertical"
    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.example.sharedpreferencestest.MainActivity" >

    <EditText 
        android:id="@+id/edit_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"/>
    <EditText 
        android:id="@+id/edit_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:password="true"
        android:background="#ffffff"/>
    <TableRow 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <CheckBox 
            android:id="@+id/remember_password"
            android:layout_height="wrap_content"/>
        <TextView 
            android:layout_height="wrap_content"
            android:text="记住密码"/>
    </TableRow>
    <Button 
        android:id="@+id/button_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="登录"
        android:background="#3DCC79"/>
</LinearLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值