开发者

getText from an EditText that is updated

I have a layout where I have an EditText with a predefined text.

Then I have a button that, when is pressed, displays a dialog with another EditText that gets the text of the previous EditText.

This works fine. But when I edit the first EditText, and press the button again, it displays the old text, not the new.

How can I get the updated text?

This is the code (inside onCreateDialog) where I get the text from the first EditText:

final View notesDialogLayout = getLayoutInflater().inflate(R.layout.notes_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(notesDialogLayout)
       .setCancelable(false)
       .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
               dialog.cancel();
           }
  开发者_如何学Python     })

AlertDialog alert = builder.create();

mNotesEdit = (EditText) notesDialogLayout.findViewById(R.id.task_notes_dialog);
mNotesEdit.setText(mNotes.getText());

mNotes is just an EditText.

The first time the Dialog opens, it gets the updated text. From the second time, it gets always the same text, even if I edit it.


You are using onCreateDialog. This method is called once only, you must use onPrepareDialog to set your text in the dialog, which is called each time you call showDialog().

edit

protected void onCreateDialog(int dialogId) {
    View notesDialogLayout = getLayoutInflater().inflate(R.layout.notes_dialog, null);
    return new AlertDialog.Builder(this)
            .setView(notesDialogLayout)
            .setCancelable(false)
            .setPositiveButton(R.string.save, null)
            .create();
}

protected void onPrepareDialog(Dialog dialog) {
    EditText content = (EditText) dialog.findViewById(R.id.task_notes_dialog);
    content.setText(mNotes.getText());
}


Here is the code that works

      package com.hollow;

    import android.app.Activity;
    import android.os.Bundle;
  import android.view.View;
   import android.view.View.OnClickListener;
  import android.widget.Button;
    import android.widget.EditText;
   import android.widget.Toast;

public class HellowActivity extends Activity {
/** Called when the activity is first created. */

EditText editText;
EditText editText2;
Button b;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    editText =(EditText) findViewById(R.id.editText1) ;
    editText2 = (EditText) findViewById(R.id.editText2);
    b = (Button) findViewById(R.id.button1);
    b.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            editText2.setText(editText.getText());

        }
    });
}
     }


Yes it displays old value because you enter predefine text in your xml file code. for solve this.. you have to handle your edittext text string into your java file. update the that string variable.

eg: String myTextInput = "Predefined text";
    EditText edt = (EditText) findViewId(R.id.edt);
    edt.setText(myTextInput);

now when u change the text then update the value on your variable like :

myTextInput = edt.getText();


I have tried to implement onPrepareDialog but I did not have luck, because I didn't find how to update something that is inside the dialog (like the EditText in my case).

So I ended putting this in my dialog's button:

removeDialog(NOTES_DIALOG_ID);

And now it is always getting the updated text, as the dialog is removed every time it gets closed.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜