问题
I have searched for solutions to this problem but none seem to work. I want to save an input from EditText to SharedPreferences and when I return to the page I would like that input to remain there until cleared.
I have tried to do it like this but it won't work and I am unsure why
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data);
loadStepPref();
final Button saveStepLength = (Button) findViewById(R.id.saveStepButton);
if(saveStepLength != null) {
saveStepLength.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.editStepLength);
saveStepLength.setVisibility(View.GONE);
//Hide keypad
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
saveStepPref("editStepLength", null);
}
});
}
private void loadStepPref() {
EditText inputStepLength = (EditText) findViewById(R.id.editStepLength);
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String value = sp.getString("editStepLength", null);
inputStepLength.setText(value);
}
private void saveStepPref(String key, String value){
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor edit = sp.edit();
edit.putString(key, value);
edit.apply();
}
}
Here is the section of the xml:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/step_length_data"
android:id="@+id/textView12"
android:layout_gravity="center_horizontal"
android:layout_marginTop="41dp"
android:layout_alignParentTop="true"
android:layout_toStartOf="@+id/returnHomeButton" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@id/textView12"
android:inputType="number"
android:layout_alignBottom="@id/textView12"
android:id="@+id/editStepLength"
android:hint = "@string/step_length_entry"/>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save"
android:id="@+id/saveStepButton"
android:layout_alignTop="@+id/editStepLength"
android:layout_toEndOf="@+id/editStepLength" />
回答1:
You never get it work .. because you are saving null always !
saveStepPref("editStepLength", null);
changed it to
saveStepPref("editStepLength", edittext.getText().toString());
回答2:
Shared prefs is something which you can not avoid using on any android app, therefore I would highly recommend creating a very nice reusable SharedPreFsHelper Class. Look at the example below.
create a java class with SharedPrefConstants as arranged below.
interface SharedPrefConstants {
String DEFAULT_SP_KEY = "default_sp_key";
String EDIT_STEP = "editStep";
}
public class SharedPrefsHelper implements SharedPrefConstant{
public static void setEditStepLength (Context context, String length) {
SharedPreferences sp = context.getSharedPreferences(DEFAULT_SP_KEY, 0);
SharedPreferences.Editor editor = sp.edit();
editor.putString(EDIT_STEP, length);
editor.commit();
}
public static String getEditStepLength (Context context) {
SharedPreferences sp = context.getSharedPreferences(DEFAULT_SP_KEY, 0);
return sp.getString(EDIT_STEP, "");
}
}
now in your project do something like
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data);
loadStepPref();
final Button saveStepLength = (Button) findViewById(R.id.saveStepButton);
if(saveStepLength != null) {
saveStepLength.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.editStepLength);
saveStepLength.setVisibility(View.GONE);
//Hide keypad
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
SharedPrefsHelper.setEditStepLength(this, "editStepLength");
}
});
}
private void loadStepPref() {
EditText inputStepLength = (EditText) findViewById(R.id.editStepLength);
inputStepLength.setText(SharePrefsHelper.getEditStepLength(this));
}
}
来源:https://stackoverflow.com/questions/38339982/save-number-input-from-edittext-into-sharedpreferences