Android:SharedPreferences 在您取消而不是关闭应用程序时额外添加 4 个空格
Android: SharedPreferences adds an extra 4 blanks when you cancel an app instead of close it
问题详情
看过
- API30,Android工作室模拟器
- API23、小米红米3s
- API10、三星 Galaxy Ace 2
打开应用程序。
输入“abc”和“\n”(换行符)。
关闭应用程序。
再次打开它。
预计:
EditText 包含“abc\n”。
观察到:
不出所料。
现在在任务管理器中取消应用。
再次打开它。
预计:
EditText 包含“abc\n”。
观察到:
EditText 包含“abc\n”。
'\n'后面的这4个空格是哪里来的?
我的代码
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText note;
private SharedPreferences prefs;
private SharedPreferences.Editor prefEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = getPreferences(Context.MODE_PRIVATE);
prefEditor = prefs.edit();
note = new EditText(this);
setContentView(note);
}
@Override
protected void onResume() {
super.onResume();
note.setText(prefs.getString("note", ""));
}
@Override
protected void onPause() {
prefEditor.putString("note", note.getText().toString());
prefEditor.commit();
super.onPause();
}
}
不完美但合理的解决方案
在 onPause() 中 - 正如 @Harsh0021 所建议的那样 - 我将使用 trim() 删除所有前导和尾随白色 space。然后在 onResume() 中,我将添加一个 '\n' 以防 SharedPreferences returns 任何文本。
顺便说一句,我使用 commit() 而不是 apply() 因为它足够好并且 API 1 而不是 9.
嘿嘿!!
第一个
改变
@Override
protected void onPause() {
prefEditor.putString("note", note.getText().toString());
prefEditor.commit();
super.onPause();
}
到
@Override
protected void onPause() {
prefEditor.putString("note", note.getText().toString().trim());
prefEditor.apply();
super.onPause();
}
那么,就这样了
问题详情
看过
- API30,Android工作室模拟器
- API23、小米红米3s
- API10、三星 Galaxy Ace 2
打开应用程序。 输入“abc”和“\n”(换行符)。 关闭应用程序。 再次打开它。
预计: EditText 包含“abc\n”。
观察到: 不出所料。
现在在任务管理器中取消应用。 再次打开它。
预计: EditText 包含“abc\n”。
观察到: EditText 包含“abc\n”。
'\n'后面的这4个空格是哪里来的?
我的代码
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
public class MainActivity extends Activity {
private EditText note;
private SharedPreferences prefs;
private SharedPreferences.Editor prefEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = getPreferences(Context.MODE_PRIVATE);
prefEditor = prefs.edit();
note = new EditText(this);
setContentView(note);
}
@Override
protected void onResume() {
super.onResume();
note.setText(prefs.getString("note", ""));
}
@Override
protected void onPause() {
prefEditor.putString("note", note.getText().toString());
prefEditor.commit();
super.onPause();
}
}
不完美但合理的解决方案
在 onPause() 中 - 正如 @Harsh0021 所建议的那样 - 我将使用 trim() 删除所有前导和尾随白色 space。然后在 onResume() 中,我将添加一个 '\n' 以防 SharedPreferences returns 任何文本。
顺便说一句,我使用 commit() 而不是 apply() 因为它足够好并且 API 1 而不是 9.
嘿嘿!!
第一个
改变
@Override
protected void onPause() {
prefEditor.putString("note", note.getText().toString());
prefEditor.commit();
super.onPause();
}
到
@Override
protected void onPause() {
prefEditor.putString("note", note.getText().toString().trim());
prefEditor.apply();
super.onPause();
}
那么,就这样了