Android之SharePreferences

布局文件:
<?xml version="1.0" encoding="utf-8"?>
<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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"/>

        <EditText
            android:id="@+id/user"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入你的用户名!"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"/>

        <EditText
            android:id="@+id/psw"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入你的密码!"/>
    </LinearLayout>

    <CheckBox
        android:id="@+id/remember"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="记住密码"/>

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="登录"/>
</LinearLayout>
package com.duolyn.administrator.logintest;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import static com.duolyn.administrator.logintest.R.id.login;
import static com.duolyn.administrator.logintest.R.id.psw;
import static com.duolyn.administrator.logintest.R.id.user;

public class MainActivity extends AppCompatActivity {

    private SharedPreferences sp;
    private EditText user_et, psw_et;
    private Button btn;
    private CheckBox checkBox;
    private Editor editor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sp = getSharedPreferences("test", Activity.MODE_PRIVATE); //私有数据
        editor = sp.edit();//获取编辑器

        user_et = (EditText) findViewById(R.id.user);
        psw_et = (EditText) findViewById(R.id.psw);
        checkBox = (CheckBox) findViewById(R.id.remember);
        btn = (Button) findViewById(R.id.login);

        String user = sp.getString("user", null);
        String psw = sp.getString("psw", null);


        if (user == null && psw == null) {
            checkBox.setChecked(false);
            user_et.setText(user);
            psw_et.setText(psw);
        }
        else {
            checkBox.setChecked(true);
            user_et.setText(user);
            psw_et.setText(psw);
        }

    }
    public void doClick(View v) {
        if (checkBox.isChecked()) {
            String username = user_et.getText().toString();//获取用户名
            String password = psw_et.getText().toString();//获取密码
            editor.putString("user", username);
            editor.putString("psw", password);
            editor.commit();
            Toast.makeText(getApplicationContext(), "登录成功!", Toast.LENGTH_SHORT).show();
        } else {
            editor.remove("user");
            editor.remove("psw");
            editor.commit();
            Toast.makeText(getApplicationContext(), "未记住密码!", Toast.LENGTH_SHORT).show();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值