将EditText中的数字输入保存到SharedPreferences中

时间:2016-07-12 22:40:45

标签: android android-edittext sharedpreferences

我已经搜索了这个问题的解决方案但似乎没有工作。我想保存从EditText到SharedPreferences的输入,当我返回到页面时,我希望该输入保持在那里直到清除。

我试图这样做,但它不起作用,我不确定为什么

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();
    }
}

以下是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" />

2 个答案:

答案 0 :(得分:1)

你永远不会得到它...因为你总是保存null!

 saveStepPref("editStepLength", null);

将其更改为

 saveStepPref("editStepLength", edittext.getText().toString());

答案 1 :(得分:1)

共享首选项是你无法避免在任何Android应用程序上使用的东西,因此我强烈建议创建一个非常好的可重用的SharedPreFsHelper类。请看下面的例子。

使用SharedPrefConstants创建一个java类,如下所示。

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, "");
   }
}

现在在您的项目中执行类似

的操作
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));
  }
}