掌握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;
}
}
时间的箭头
都指向你铩羽而归的地方
你会前进
但终究还是得要习惯投降
想当然尔
第六步是你最大的致命伤
我按兵不动
出于习惯凡事沉默的酝酿
当头炮 纯粹出于我礼貌的开场
屏风马 神华内敛才能以柔克刚