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

Save entered text in editText via button(通过按钮将输入的文本保存在editText中)

本文介绍了通过按钮将输入的文本保存在editText中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 sharedPreferences 保存 EditText 中输入的 String 值.并在活动开始时显示文本

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

            }


        }

    };

}

推荐答案

你必须用下面两行注释来改变你的代码:

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

            }
        }
    };
}

这篇关于通过按钮将输入的文本保存在editText中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:通过按钮将输入的文本保存在editText中

基础教程推荐