【问题标题】:android R.string. retrieving value [duplicate]安卓 R.string。检索值[重复]
【发布时间】:2019-12-22 00:27:40
【问题描述】:

我正在练习我的 Java,并且正在尝试制作简单的文本游戏。现在有两个主要角色,每个人都有自己的弹出文本。 我已经将文本放在 string.xml 中,它们有自己的名称,例如“dialog1”“dialog2”“dialog3”,我想用 R.string.dialog* 检索字符串,但我想创建一些函数来更改数字,例如“对话” + int

如何制作这样的东西??

如果你点击屏幕上显示的“dialog1”按摩,当你第二次点击时,另一个角色应该显示下一部分“dialog2”。现在我不想手动 r.string.dialog1 然后 r.string.dialog2 我想实现这个功能,但我不知道如何传递值“dialog”+ int。

【问题讨论】:

  • 不确定你想做什么,但也许getResources().getString() 方法可以帮助你实现你所需要的。
  • 最终,可能有一些更有效的方法来做你正在尝试的事情,但要具体做你所要求的,你可以使用Resources#getIdentifier() 方法来获取int ID对于给定的字符串资源名称,然后将该 ID 与 getString() 一起使用以获取实际值。链接的副本中有几个示例。
  • @Jorgesys 是的,但请注意他们说“我不想手动执行 r.string.dialog1 然后 r.string.dialog2”。这基本上就是您的答案正在做的事情,只是回旋处。我链接了重复项以执行他们在问题中特别提出的要求,但如果他们更喜欢 R.strings 的数组,那么也有重复项。

标签: android xml string game-physics


【解决方案1】:

您可以按照本文Are parameters in strings.xml possible?中的说明在xml中放置一个参数

您需要在字符串资源中的任意位置添加 %1$d,以便在要获取字符串时发送参数。

<string name="dialog1"> This is %1$d</string>

当你想得到它时

getString(R.string.dialog1, "1")

如果需要更多参数,请更改参数编号

<string name="parameters"> Many parameters %1$d %2$d %3$d</string>

然后

getString(R.string.parameters, "1", "2", "3")

【讨论】:

    【解决方案2】:

    如果您需要格式化您的字符串,那么您可以通过将您的格式参数放入字符串资源中来实现,如以下示例资源所示。

    <string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
    

    在本例中,格式字符串有两个参数:%1$s 是字符串,%2$d 是十进制数。然后,通过调用 getString(int, Object...) 格式化字符串。例如:

    String text = getString(R.string.welcome_messages, username, mailCount);
    

    引用自:Android Documentation

    【讨论】:

      【解决方案3】:

      如果您想要一种方法来加载存储在strings.xml 中的值,则必须创建一个包含资源 ID 的整数数组。

      例子:

      strings.xml中创建消息:

      <resources>
          ...
          ...
          <string name="dialog1">My first message</string>
          <string name="dialog2">My second message</string>
          <string name="dialog3">My third message</string>
      </resources>
      

      然后使用此方法根据定义到数组中的元素的索引加载值:

      int[] messageValues = new int[] {R.string.dialog1,R.string.dialog2,R.string.dialog3};
      
      private String getDialogMessage(Context context, int dialogIndex){
          if(dialogIndex >= messageValues.length) //Check if element exist in array
              return "";
      
          return  context.getResources().getString(messageValues[dialogIndex]);
      
      }
      

      这些是如何使用该方法的示例:

              System.out.println(getDialogMessage(getApplicationContext() , 0)); //get first element message.
              System.out.println(getDialogMessage(getApplicationContext() , 1)); //get second element message.
              System.out.println(getDialogMessage(getApplicationContext() , 2)); //get third element message.
              System.out.println(getDialogMessage(getApplicationContext() , 14)); //returns "" , because the element doesnt exist into the array..
      

      【讨论】:

        猜你喜欢
        • 2014-04-02
        • 1970-01-01
        • 2013-02-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-28
        • 1970-01-01
        相关资源
        最近更新 更多