Android—— SharedPreferences存放

    掌握SharedPreferences存放数据方式
    掌握SharedPreferences存入数据的相关方法
    掌握SharedPreferences取出数据的相关方法
通过线性布局和相对布局来搭建一个用户登录界面,界面可自主设计,但至少需要包括以下控件:1个ImageView控件、2个TextView控件; 2个EditText控件、 1个Button控件。
创建工具类SPSaveLogin,在该类中实现保存与获取登录账号和密码的功能,使用SharedPreferences存储的方式保存与读取登录账号与密码的数据。运行程序,输入账号和密码信息,点击“登录”按钮,实现登录功能。

布局与控件的用法
掌握SharedPreferences存储方式,将数据存入SharedPreferences中、读取
或删除SharedPreferences中数据。

activity_main.xml

<?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="match_parent">
    <ImageView
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@drawable/jay"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:id="@+id/iv" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LL1"
        android:layout_below="@+id/iv"
        android:layout_marginTop="30dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="账号:"
            android:layout_marginLeft="10dp"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et1"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LL2"
        android:layout_below="@+id/LL1">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:layout_marginLeft="10dp"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:password="true"
            android:id="@+id/et2"/>

    </LinearLayout>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:layout_below="@+id/LL2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:id="@+id/btn"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn"
        android:id="@+id/tv_account"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:textColor="#00f"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/tv_account"
        android:id="@+id/tv_password"
        android:layout_marginLeft="10dp"
        android:textColor="#f00"/>
</RelativeLayout>

 MainActivity.java

package com.example.shiyan4;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private EditText ed_account;
    private EditText ed_password;
    private Button btn;
    private TextView tx1;
    private TextView tx2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed_account=findViewById(R.id.et1);
        ed_password=findViewById(R.id.et2);
        btn=findViewById(R.id.btn);
        tx1=findViewById(R.id.tv_account);
        tx2=findViewById(R.id.tv_password);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String account=ed_account.getText().toString();
                String password=ed_password.getText().toString();
                if (account.isEmpty()){
                    Toast.makeText(MainActivity.this,"请输入账号",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(password.isEmpty()){
                    Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                    return;
                }
                boolean isSaveSuccess=SPSaveLogin.saveUserInfo(MainActivity.this,account,password);
                if(isSaveSuccess){
                    Toast.makeText(MainActivity.this, "保存成功",Toast.LENGTH_SHORT).show();
                }
                Map<String,String> myMap=SPSaveLogin.getUserInfo(MainActivity.this);
                tx1.setText(myMap.get("account"));
                tx2.setText(myMap.get("password"));
            }
        });
    }
}

SPSaveLogin.java

package com.example.shiyan4;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;

public class SPSaveLogin {
    public static boolean saveUserInfo(Context context,String account,String password){
        SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor= sp.edit();
        editor.putString("account",account);
        editor.putString("password",password);
        editor.commit();
        return true;
    }
    public static Map<String,String>getUserInfo(Context context){
        SharedPreferences sp=context.getSharedPreferences("data",Context.MODE_PRIVATE);
        String account= sp.getString("account",null);
        String password=sp.getString("password",null);
        Map<String,String>myMap=new HashMap<>();
        myMap.put("account",account);
        myMap.put("password",password);
        return myMap;
    }
}

时间的箭头     

都指向你铩羽而归的地方

你会前进     

但终究还是得要习惯投降

想当然尔     

第六步是你最大的致命伤

我按兵不动   

出于习惯凡事沉默的酝酿

当头炮     纯粹出于我礼貌的开场

屏风马     神华内敛才能以柔克刚

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轨迹_6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值