【问题标题】:How to pass parameters to strings resource which contains link in Android如何将参数传递给包含Android中链接的字符串资源
【发布时间】:2020-08-05 11:40:48
【问题描述】:

我遇到了以下问题:存在包含 URL 的字符串资源:

<string name="msg_more_information_about_credit"><![CDATA[<b>Ejemplo TAE:</b> 300€ a 62 días. <b>Coste préstamo:</b> 148€. <b>Total a devolver:</b> 448€. TAE 2035%. <a href="https://www.moneyman.es/secci.php?pers=0.011&tae=3112.64&back=290&back_date=22.11.2020&current_time=30&current_sum=290&is62=false">Más Información </a> ]]></string>

我需要使这个链接动态化(从代码中传递值给它)。为此,我对其进行了一些修改:

<string name="msg_more_information_about_credit"><![CDATA[<b>Ejemplo TAE:</b> 300€ a 62 días. <b>Coste préstamo:</b> 148€. <b>Total a devolver:</b> 448€. TAE 2035%. <a href="https://www.moneyman.es/secci.php?pers=0.011&tae=3112.64&back=290&back_date=%1$s&current_time=30&current_sum=290&is62=false">Más Información </a> ]]></string>

然后我像往常一样尝试获取资源,当字符串包含其他字符串时:

getString(R.string.msg_more_information_about_credit, "22.11.2020")

但在这种情况下,链接没有我的设置值,而是包含“%1$s”

所以,我的问题是,如何从代码中修改链接?提前致谢!

【问题讨论】:

  • 你想动态传递哪些数据?看不到“%1$s”
  • 您是否尝试将formatted="false" 添加到您的字符串资源中? stackoverflow.com/a/12627660
  • @ShaluTD,我正在尝试将字符串传递给 back_date 参数
  • @BrunoBieri 添加了formatted="false",但结果是一样的
  • 您是否也尝试过单独读取字符串并在第二步中使用String.format(getString(..), "22.11.2020") 对其进行格式化?

标签: android


【解决方案1】:

第 1 步:string.xml 文件中添加带有链接的字符串。

<string name="mystringwithlink">Here is code how to put link into string xml \n<a href="https://stackoverflow.com">stackoverflow.com</a></string>

第 2 步: 然后在 your_activity.java 中输入以下代码行

val linkTextView = findViewById<TextView>(R.id.activity_main_link)
    
linkTextView.setMovementMethod(LinkMovementMethod.getInstance());

如果作品完美,请点赞鼓励。

参考:link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-14
    • 2021-10-16
    • 2013-09-06
    • 1970-01-01
    • 2018-09-13
    相关资源
    最近更新 更多