如何访问AlertDialog.Builder构建的AlertDialog消息? [英] How to access message of AlertDialog built by AlertDialog.Builder?

查看:112
本文介绍了如何访问AlertDialog.Builder构建的AlertDialog消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个AlertDialog和AlertDialog.Builder:

I have this AlertDialog and AlertDialog.Builder:

    String message = "A message";
    AlertDialog.Builder dBuilder = new AlertDialog.Builder(this);
    dBuilder.setMessage(message);
    ....
    AlertDialog myDialog = dBuilder.create();

现在,我正在编写单元测试,并且需要测试对话框的消息.我如何访问该消息?

Now I am writing a unit test and need to test the message of the dialog. How can I access that message?

推荐答案

试一下.不用使用dBuilder.create()初始化myDialog,而是使用构建器的构造:

Give this a shot. Instead of initializing myDialog with dBuilder.create(), use the construction of the builder:

String message = "A message";
AlertDialog.Builder dBuilder = new AlertDialog.Builder(this);
AlertDialog myDialog = dBuilder
              .setMessage(message)
              .show();
TextView messageTextView = (TextView)myDialog.findViewById(android.R.id.message);
String dialogMessage = messageTextView.getText();

这篇关于如何访问AlertDialog.Builder构建的AlertDialog消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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