通过按钮将输入的文本保存在editText中

时间:2013-09-06 12:52:08

标签: android string button android-edittext sharedpreferences

我想保存String使用sharedPreferences在EditText中使用的值。 并在活动开始时显示文字

public class enteredText extends Activity
{

  private Button savenotebutton1;
  private SharedPreferences savednotes;
  private EditText editText1;

  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.x1);

    savenotebutton1 = (Button) findViewById(R.id.savenotebutton1);
    editText1 = (EditText) findViewById(R.id.noteEditText1);
    savednotes = getSharedPreferences("notes",MODE_PRIVATE);

    savenotebutton1.setOnClickListener(saveButtonListener);
  }

  private void makeTag(String tag)
  {
       String or = savednotes.getString(tag, null);
        SharedPreferences.Editor preferencesEditor = savednotes.edit();
        preferencesEditor.putString(tag,null);
        preferencesEditor.commit();
  }


    public OnClickListener saveButtonListener = new OnClickListener(){

        @Override
        public void onClick(View v) {
            if(editText1.getText().length()>0){
            makeTag(editText1.getText().toString());    

                ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(editText1.getWindowToken(),0);

            }


        }

    };

}

2 个答案:

答案 0 :(得分:3)

您必须使用以下评论的两个新行更改您的代码:

public class enteredText extends Activity{

    private Button savenotebutton1;
    private SharedPreferences savednotes;
    private EditText editText1;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.x1);

        savenotebutton1 = (Button) findViewById(R.id.savenotebutton1);
        editText1 = (EditText) findViewById(R.id.noteEditText1);
        savednotes = getSharedPreferences("notes",MODE_PRIVATE);

        editText1.setText(savednotes.getString("tag", "Default Value")); //add this line

        savenotebutton1.setOnClickListener(saveButtonListener);
    }

    private void makeTag(String tag){
       String or = savednotes.getString(tag, null);
        SharedPreferences.Editor preferencesEditor = savednotes.edit();
        preferencesEditor.putString("tag",tag); //change this line to this
        preferencesEditor.commit();
    }

    public OnClickListener saveButtonListener = new OnClickListener(){

        @Override
        public void onClick(View v) {
            if(editText1.getText().length()>0){
            makeTag(editText1.getText().toString());    

            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(editText1.getWindowToken(),0);

            }
        }
    };
}

答案 1 :(得分:1)

更改您的功能:

private void makeTag(String tag){
        String or = savednotes.getString("KEY_NAME", "0");// here, '0' is default value
        Editor preferencesEditor = savednotes.edit();
        preferencesEditor.putString("KEY_NAME",tag);
        preferencesEditor.commit();

    }

修改

onCreate()
{
savednotes=PreferenceManager.getDefaultSharedPreferences(getBaseContext());
 }