用户登录记住密码--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);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值