一.声明控件
1.按钮
<Button
android:id="@+id/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.766"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.215" />
2.文字
<TextView
android:id="@+id/lab1"
android:layout_width="132dp"
android:layout_height="76dp"
android:text="test"
android:textSize="66sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.744" />
3.输入框
<EditText
android:id="@+id/Textbox"
android:layout_width="115dp"
android:layout_height="51dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.125"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.404" />
二.设置按钮
1.声明按钮
Button button = (Button) findViewById(R.id.SaveButton);
2.设置关联
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
三.读取数据
1.实例化输入框
EditText TextBox1 = (EditText) findViewById(R.id.Textbox1);
2.获取数据
String str = TextBox1.getText().toString();
四.界面
1.取消默认的头部显示
将res —>values —>themes.xml的
parent="Theme.MaterialComponents.DayNight.DarkActionBar">
改为
parent="Theme.AppCompat.DayNight.NoActionBar">
五.数据
1.保存键值对
//获取一个 SharedPreferences对象
//第一个参数:指定文件的名字,只会续写不会覆盖
//第二个参数:MODE_PRIVATE只有当前应用程序可以续写
SharedPreferences.Editor editor=getSharedPreferences(path,MODE_PRIVATE).edit();
//向其中添加数据,是什么数据类型就put什么,前面是键,后面是数据
editor.putString(key,value);
//调用apply方法将添加的数据提交,从而完成存储的动作
editor.apply();
2.读取键值对
//首先获取一个SharedPreferences对象
SharedPreferences preferences=getSharedPreferences(key,MODE_PRIVATE);
//然后通过键的方式取出,后边是如果找不到的默认内容
String amount=preferences.getString(value,"");
3.读取所有键值对
SharedPreferences sharedPreferences = getSharedPreferences("your_prefs_name", MODE_PRIVATE);
Map<String, ?> allEntries = sharedPreferences.getAll();
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
// 根据值的类型进行转换和打印
if (value instanceof String) {
String stringValue = (String) value;
Log.d("SharedPreferences", "Key: " + key + ", Value: " + stringValue);
} else if (value instanceof Integer) {
int intValue = (Integer) value;
Log.d("SharedPreferences", "Key: " + key + ", Value: " + intValue);
} else if (value instanceof Boolean) {
boolean booleanValue = (Boolean) value;
Log.d("SharedPreferences", "Key: " + key + ", Value: " + booleanValue);
} else if (value instanceof Float) {
float floatValue = (Float) value;
Log.d("SharedPreferences", "Key: " + key + ", Value: " + floatValue);
} else if (value instanceof Long) {
long longValue = (Long) value;
Log.d("SharedPreferences", "Key: " + key + ", Value: " + longValue);
} else if (value instanceof Set<?>) {
// 对于Set类型,你可能需要进一步处理
Set<?> setValue = (Set<?>) value;
Log.d("SharedPreferences", "Key: " + key + ", Value (Set): " + setValue.toString());
}
// 注意:SharedPreferences不支持直接存储自定义对象,这里只是展示了可能的类型检查
}
4.删除所有键值对
SharedPreferences sharedPreferences = getSharedPreferences("your_prefs_name", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear(); // 清除所有的键值对
editor.apply(); // 提交更改
// 或者使用 editor.commit();,但 apply() 是异步的,通常更推荐使用
六.打包APK
1.点击导航栏的Build→Generate Signed Bundle / APK…
然后在项目文件夹中\app\release就可以找到了