Android的存储 SharedPreferences和SQLite数据库
package com.zlj.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity4 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main4);
//数据的存储
//模拟登录是记录账号密码功能
String user = "root";
String pwd = "123456";
//数据的写入
//1. 获取SharedPreferences
SharedPreferences sp = getSharedPreferences("loginfo",MODE_PRIVATE);
//2.借助Editor,向SharedPreferences中写入数据
SharedPreferences.Editor editor = sp.edit();
//3.写入数据 只能写入原始基本数据类型,不能写入复合型数据
editor.putString("user",user);
editor.putString("pwd",pwd);
//4.提交数据
editor.commit();
//数据的读取
//1.获取SharedPreferences
SharedPreferences sp1 = getSharedPreferences("loginfo",MODE_PRIVATE);
//2.获取数据
String sp_user = sp.getString("user","");
String sp_pwd = sp.getString("pwd","");
Toast.makeText(this, "账号:"+sp_user+"密码:"+sp_pwd, Toast.LENGTH_SHORT).show();
}
}