布局文件activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity"
android:paddingHorizontal="10dp"
android:gravity="center">
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="名称"
android:inputType="text"
android:paddingVertical="10dp"
android:paddingStart="10dp"
android:textSize="26sp"/>
<EditText
android:id="@+id/et_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_name"
android:layout_marginTop="20dp"
android:hint="数量"
android:inputType="text"
android:paddingVertical="10dp"
android:paddingStart="10dp"
android:textSize="26sp"/>

<LinearLayout
android:id="@+id/lyt_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_num"
android:layout_marginTop="20dp"
android:gravity="center">

<Button
android:id="@+id/btn_save"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginHorizontal="2dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="保存"
android:textSize="26sp" />

<Button
android:id="@+id/btn_read"
android:layout_width="0dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="读取"
android:textSize="26sp" />

<Button
android:id="@+id/btn_remove"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginHorizontal="2dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="删除"
android:textSize="26sp" />

<Button
android:id="@+id/btn_clear"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginHorizontal="2dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="清除"
android:textSize="26sp" />
</LinearLayout>


<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/lyt_btn"
android:layout_marginTop="20dp"
android:maxLines="5"
android:paddingVertical="20dp"
android:paddingStart="10dp"
android:textSize="30sp"/>


</RelativeLayout>

Java代码MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.example.datademo;

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity {
private EditText mEtName, mEtNumber;
private Button mBtnSave, mBtnRead, mBtnRemove, mBtnClear;
private TextView mTv;
private SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBtnSave = findViewById(R.id.btn_save);
mBtnRead = findViewById(R.id.btn_read);
mBtnRemove = findViewById(R.id.btn_remove);
mBtnClear = findViewById(R.id.btn_clear);
mTv = findViewById(R.id.tv);
mEtName = findViewById(R.id.et_name);
mEtNumber = findViewById(R.id.et_num);
sharedPreferences = getSharedPreferences("Order", MODE_PRIVATE);

mBtnSave.setOnClickListener((View view)->dataSave());
mBtnRead.setOnClickListener((View view)->dataRead());
mBtnRemove.setOnClickListener((View view)->dataRemove("name"));
mBtnClear.setOnClickListener((View view)->dataClear());
}

private void dataClear() {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
}

private void dataRemove(String name) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(name);
editor.apply();
}

private void dataSave() {
SharedPreferences.Editor editor = sharedPreferences.edit();
String name = mEtName.getText().toString();
editor.putString("name", name);
String numberText = mEtNumber.getText().toString();
int number = Integer.parseInt(numberText);
editor.putInt("number",number);
editor.putBoolean("pay", true);

editor.apply();
}

private void dataRead() {
String name = sharedPreferences.getString("name","defaultName");
int number = sharedPreferences.getInt("number", 0);
boolean pay = sharedPreferences.getBoolean("pay", false);

mTv.append(String.format("%s---%d---%s\n", name,number,pay));
}
}

运行结果

输入如图所示,使用以下操作,分别得到三种输出结果在TextView中显示

  1. 保存->读取
  2. 删除->读取
  3. 清除->读取