Android SharedPreferences数据库

效果图:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
MainActivity.java

ublic class MainActivity extends AppCompatActivity {
    private EditText edtName,edtWord;   // 账号,密码
    private Button button;      //  登录
    private String username,password;   //账号,密码

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

        edtName = findViewById(R.id.edt_name);
        edtWord = findViewById(R.id.edt_word);
        button = findViewById(R.id.btn_tiao);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();     //  存储 账号、密码 的值
                Intent intent = new Intent(MainActivity.this,TwoActivity.class);
                startActivity(intent);      //  跳转界面
            }
        });

    }

    private void initData() {

        username = edtName.getText().toString();
        password = edtWord.getText().toString();

        /**
         *      我们在存值前,需要一个 Editor 对象。
         *      得到 Editor 对象后,我们才能进行存值
         *      第一个参数是: SharedPreferences 文件的名称
         *      第二个参数是: 指定操作模式,用 MODE_PRIVATE 即可
         */
        SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();

        editor.putString("name",username);      //  存储账号
        editor.putInt("word",Integer.parseInt(password));       //  将字符串转化为整型,并存储到 word 键 中

        editor.apply();     //提交
    }
}

TwoActivity.java

public class TwoActivity extends AppCompatActivity {
    private Button btnZhang,btnMi;
    String username;
    int password;

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

        btnZhang = findViewById(R.id.btn_zhang);
        btnMi = findViewById(R.id.btn_mi);

        //  取出 SharedPreferences 存储的值,就要先实例化出这个对象
        SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);
        username = pref.getString("name","");
        password = pref.getInt("word",0);

        btnZhang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TwoActivity.this,username,Toast.LENGTH_LONG).show();
            }
        });

        btnMi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TwoActivity.this,""+password,Toast.LENGTH_LONG).show();
            }
        });
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edt_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="请输入账号"/>

    <EditText
        android:id="@+id/edt_word"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:hint="请输入密码"/>

    <Button
        android:id="@+id/btn_tiao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:layout_gravity="center"
        android:text="跳转"/>
    
</LinearLayout>

activity_two.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".TwoActivity">

    <Button
        android:id="@+id/btn_zhang"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="60dp"
        android:text="账号"/>

    <Button
        android:id="@+id/btn_mi"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="60dp"
        android:text="密码"/>
    
</LinearLayout>

总结:

一、 如何存值:

	我们需要先得到一个 Editor 对象,再利用对应的类型按照键值的关系进行存储,代码如下:
	
	SharedPreferences.Editor editor = getSharedPreferences("data",MODE_PRIVATE).edit();
    editor.putString("name",username); 

二丶 小心的地方:

		记得在存值逻辑写完后,再加个提交代码
		
	     editor.apply();     //提交

三丶 如何取值:
		
		得到一个 SharedPreferences 对象,按照键值关系取出即可
		
 	SharedPreferences pref = getSharedPreferences("data",MODE_PRIVATE);
 	username = pref.getString("name","");

如果有问题,可以联系我qq:1787424177

最后,我真诚的希望能评论一句嘛,让我知道你来过,我会很开心的

  • 8
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

王睿丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值