Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

android - Save entered text in editText via button

i want to save a String's value that is entenred in EditText using sharedPreferences. and show text when activity starts

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

            }


        }

    };

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have to change your code with two new lines with comments below:

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

            }
        }
    };
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...