数据存储(慕课)SharedPreferences 保存和读取

 

 

 

 案例演示   

SharedPreferences 保存和读取
package com.example.testapplication;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class ShareActivity extends ApplicationActivity{
    private EditText accEdt,pwdEdt;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share);
        accEdt = findViewById(R.id.acc_edt);
        pwdEdt = findViewById(R.id.pwd_edt);

        //SharePreference的读取
        //①获取SharePreference对象(参数1:文件名  参数2:模式)
        SharedPreferences share = getSharedPreferences("myshare",MODE_PRIVATE);
        //②根据key获取内容(参数1:key   参数2:当对应key不存在时,返回参数2的内容作为默认值)
        String accStr = share.getString("account","");
        String pwdStr = share.getString("pwd","");
        accEdt.setText(accStr);
        pwdEdt.setText(pwdStr);


        findViewById(R.id.login_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 1. 获取两个输入框的内容
                String account = accEdt.getText().toString();
                String pwd = pwdEdt.getText().toString();

                // 2.验证(admin 123)
                if(account.equals("admin") && pwd.equals("123")){
                    // 2.1 获取存储信息到SharePreference
                    //获取SharePreference对象(参数1:文件名  参数2:模式)
                    SharedPreferences share = getSharedPreferences("myshare",MODE_PRIVATE);
                    //2.2 获取Editor对象
                    SharedPreferences.Editor edt = share.edit();
                    // 存储信息
                    edt.putString("account",account);
                    edt.putString("pwd",pwd);
                    // 提交操作
                    edt.commit();
                    Toast.makeText(ShareActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                }else{
                    //2.2验证失败,提示用户
                    Toast.makeText(ShareActivity.this,"账号或密码错误",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="50dp"
        >
        <TextView
            android:id="@+id/textView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:text="账号:"
            android:textSize="18sp"
             />

        <EditText
            android:id="@+id/acc_edt"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            />
    </LinearLayout>

    <LinearLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            android:layout_marginBottom="8dp"
            android:text="密码:"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/pwd_edt"
            android:layout_width="350dp"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:inputType="textPassword" />
    </LinearLayout>
    <Button
        android:id="@+id/login_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:layout_margin="15dp" />

</LinearLayout>

猜你喜欢

转载自blog.csdn.net/weixin_38107457/article/details/121799137