【问题标题】:How to make a dialog with content from a string?如何使用字符串中的内容进行对话?
【发布时间】:2013-01-06 19:08:56
【问题描述】:

是否可以使用 OnCreate 中定义的字符串中的内容进行对话?因为我能找到的定义对话框内容的唯一方法是使用文本或在 values/strings.xml 中定义的字符串,如下所示:

builder.setMessage(R.string.dialog_fire_missiles)

我希望有人可以帮助我,如果我的问题足够清楚,请告诉我。

【问题讨论】:

  • 对不起,我没有清楚地了解您的问题。请举个例子。

标签: android dialog android-alertdialog


【解决方案1】:

使用ContextgetString方法从strings.xml中获取字符串:

builder.setMessage(Your_Activity.this.getString(R.string.dialog_fire_missiles))

【讨论】:

    【解决方案2】:

    只需使用:

    builder.setMessage("Your Message")
    

    有两个 setMessage() 方法,one for accepting a CharSequence 和 one for accepting an R.string.*

    【讨论】:

      【解决方案3】:

      使用getString方法

      ProgressDialog pDialog;
      
      pDialog.setMessage(getString(R.string.your_xml_string));
      

      【讨论】:

        【解决方案4】:
        getApplicationContext().getString(R.string.yourstring);
        

        您的活动上下文为您提供从“R”加载资源所需的一切。

        【讨论】:

          【解决方案5】:

          是的,你可以使用字符串 o_O

          首先检查文档:setMessage (CharSequence message)

          然后可以查看教程测试对话框here

          对于懒惰的人:

          String text = "Hello";
          
                      AlertDialog.Builder builder = new AlertDialog.Builder(this);
                      builder.setMessage(text).setPositiveButton("Wohoo", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                              // FIRE ZE MISSILES!
                          }
                      }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int id) {
                              // User cancelled the dialog
                          }
                      });
          

          最后添加这个,因为你已经创建了对话框并且需要显示:

          builder.show();
          

          【讨论】:

            【解决方案6】:

            获取资源,如:

            builder.setMessage(getRessources.getString(R.string.dialog_fire_missiles))
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2016-03-18
              • 2022-11-13
              • 2016-04-11
              • 2022-12-16
              • 2015-07-03
              • 1970-01-01
              • 2021-03-12
              • 2019-07-13
              相关资源
              最近更新 更多