从更新一个EditText的getText [英] getText from an EditText that is updated

查看:120
本文介绍了从更新一个EditText的getText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个布局,我有一个predefined文本一个EditText。

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

然后我有一个按钮,当被pressed,显示一个对话框与其他的EditText是可以获得$ ​​P $ pvious的EditText文本。

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

这工作正常。但是,当我修改第一的EditText,和PSS再按下此键$ P $,它显示了旧文本,而不是新的。

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?

这是code(内onCreateDialog)在那里我得到了第一的EditText文本:

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

AlertDialog alert = builder.create();

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

mNotes只是一个EditText。

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.

推荐答案

您正在使用onCreateDialog。这种方法被称为只有一次,你必须使用在prepareDialog设置对话框中你的文字,这是每次调用的ShowDialog()时调用。

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().

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

这篇关于从更新一个EditText的getText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆