如何从Android中的AlertDialog获取文本

5
我需要你的帮助获取刷新时的字符串值,并在AlertDialog中显示它。我想在复制按钮单击时将文本复制到新字符串中,同时在AlertDialog中添加“复制”按钮。
String plain ="hello;" // changing on refresh

new AlertDialog.Builder(Demo.this)
.setTitle("New Message")
.setMessage(plain)
.setPositiveButton(android.R.string.ok, null)
.setCancelable(false)
.create().show();

http://stackoverflow.com/questions/9978393/android-get-retrieve-progressdialog-titles-id-and-dialegerror-titles-id - s_bei
3个回答

6
如果您想从警告对话框中发送消息,请尝试以下代码。
 TextView tv = (TextView)YOUR_ALERT_DIALOG.findViewById(android.R.id.message);
 Log.e(this.getClass().getName(), ""+tv.getText());

如果您的警告对话框中未设置消息,则会引发空指针异常。

0
你好 Pooja,这是你需要的代码。
public class Blah extends Activity {
private String result;

// other activity stuff

void showDialog(){

String plain="hello";

AlertDialog.Builder b = new AlertDialog.Builder(this);
b.setTitle("Please enter a password");
b.setMessage(plain)
final EditText input = new EditText(this);
b.setView(input);
b.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
    @Override
    public void onClick(DialogInterface dialog, int whichButton)
    {
       // SHOULD NOW WORK
       result = plain;
    }
});
b.setNegativeButton("CANCEL", null);
b.create().show();
}
}

我不想输入,我想从中复制。请使用 .setMessage。 - Pooja
嗨@Pooja,获取字符串在onClick监听器中非常简单,即在onClick监听器中result = plain。 - Mohan

0
添加一个中性按钮并设置它的监听器,代码如下:
final String plain ="hello;" // changing on refresh
String copy;

new AlertDialog.Builder(Demo.this)
.setTitle("New Message")
.setMessage(plain)
.setPositiveButton(android.R.string.ok, null)
.setNeutralButton("COPY", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                    copy = plain;
                }
            })
.setCancelable(false)
.create().show();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接