PLZ帮助!如何让应用程序保存用户在edittext中所写的内容



如图所示,我想将用户写的内容保存为注释。因此,当用户重新打开应用程序时,他写的笔记将保留在日历下,顺便说一句,我使用了EditText多行

https://i.stack.imgur.com/jcMcJ.jpg

package com.example.test;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText editText;
SharedPreferences sharedPreferences;
private static final String SHARED_PREF_NAME= "mypref";
private static final String KEY_NAME = "name";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=findViewById(R.id.editTextTextMultiLine);
sharedPreferences=getSharedPreferences(SHARED_PREF_NAME ,MODE_PRIVATE);
String name = sharedPreferences.getString(KEY_NAME,null);

SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(KEY_NAME, editText.getText().toString());
editor.apply();
editor.clear();
editor.commit();
}
}

我认为您应该将editor.clear()移动到editor.commit()下方

最新更新