Android-是否可以在字符串资源中添加可点击的链接

发布于 2025-01-03 11:02:36 字数 300 浏览 4 评论 0原文

我通常会设置某种 AlertDialog 在用户第一次使用我的应用程序时触发,我会解释如何使用该应用程序并对他们刚刚下载的内容进行总体介绍。我通常还从 strings.xml 文件加载字符串。

我想要做的是使我的字符串资源中的单词之一可以像网页上的超链接一样单击。基本上,您将有一个AlertDialog,并且在字符串资源中会有一个突出显示的单词,或者可能只是一个他们可以按下的网址。我想我可以添加一个按钮将它们带到该网站,但我只是想知道是否可以在字符串资源中创建一个单词作为可点击的超链接。

I usually set up some kind of AlertDialog to fire off when a user first uses one of my apps and I explain how to use the app and give an overall introduction to what they just downloaded. I also usually load my strings from a strings.xml file.

What I want to do is make one of the words in my string resource clickable like a hyperlink on a web page. Basically you'd have an AlertDialog and within the string resource there would be a highlighted word or possibly just a web address that they could press. I suppose I could just add a button that would take them to the site but I just wanted to know if making a word in your string resource a clickable hyperlink was possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寄居者 2025-01-10 11:02:36

只需在资源中使用 HTML 格式链接:

Click me!< /string>

然后,您可以在 TextView 上使用 setMovementMethod(LinkMovementMethod.getInstance()) 以使链接可点击。

还有 TextViewandroid:autoLink 属性也应该起作用。

Just use an HTML format link in your resource:

<string name="my_link"><a href="http://somesite.com/">Click me!</a></string>

You can then use setMovementMethod(LinkMovementMethod.getInstance()) on your TextView to make the link clickable.

There is also TextView's android:autoLink attribute which should also work.

悲歌长辞 2025-01-10 11:02:36

我发现了一些有趣的事情。如果有人注意到这一点,请告诉我。

如果您使用以下超链接则不起作用

android:autoLink="web"

,但可以使用

TextView link = (TextView) findViewById(R.id.link);
link.setMovementMethod(LinkMovementMethod.getInstance());

<string name="my_link">
    <a href="http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource">
        Click me!
    </a>
</string>

但如果您使用以下链接则可以同时使用

android:autoLink="web" (or)
TextView link = (TextView) findViewById(R.id.link);
link.setMovementMethod(LinkMovementMethod.getInstance());

<string name="my_link">
    <a href="http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource"> 
        http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource
    </a>
</string>

I found something interesting. Let me know if any of you observed this.

Below hyperlink is not working if you use

android:autoLink="web"

but works with

TextView link = (TextView) findViewById(R.id.link);
link.setMovementMethod(LinkMovementMethod.getInstance());

<string name="my_link">
    <a href="http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource">
        Click me!
    </a>
</string>

but if you use the following link it works with both

android:autoLink="web" (or)
TextView link = (TextView) findViewById(R.id.link);
link.setMovementMethod(LinkMovementMethod.getInstance());

<string name="my_link">
    <a href="http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource"> 
        http://stackoverflow.com/questions/9204303/android-is-it-possible-to-add-a-clickable-link-into-a-string-resource
    </a>
</string>
2025-01-10 11:02:36

正如 @Nikolay Elenkov 所回答的,在 Kotlin 中,我以这种方式从字符串资源中使用它(新手的详细方式):

在我的布局中放置一个复选框:

<CheckBox
        android:id="@+id/termsCB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="@dimen/spacing_normal"
        android:layout_marginTop="@dimen/spacing_normal"
        android:text="@string/terms_and_conditions" />

strings.xml 中

<string name="terms_and_conditions">I read and accept the <a href="http://www.spiffyshow.com/">Terms and Conditions</a></string>

在 onCreate( 内的 activity 类中的 ) 方法:

termsCB.movementMethod = LinkMovementMethod.getInstance()

As answered by @Nikolay Elenkov In Kotlin I used it from string resources in this way (detailed way for freshers):

in my layout place a checkbox:

<CheckBox
        android:id="@+id/termsCB"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="@dimen/spacing_normal"
        android:layout_marginTop="@dimen/spacing_normal"
        android:text="@string/terms_and_conditions" />

in strings.xml

<string name="terms_and_conditions">I read and accept the <a href="http://www.spiffyshow.com/">Terms and Conditions</a></string>

in my activity class inside the onCreate() method:

termsCB.movementMethod = LinkMovementMethod.getInstance()
毁梦 2025-01-10 11:02:36

对我来说,超链接总是工作得很好,当我:

  1. 将这行添加到活动/片段中:
textView.setMovementMethod(LinkMovementMethod.getInstance())
textView.setText(Html.fromHtml(getString(R.string.link)))
  1. 不要向 xml 添加任何内容(如自动链接等)。
  2. strings.xml 中定义链接,例如

Google链接]]>

For me, the hyperlink always works well, when I:

  1. Add these two lines to the activity/fragment:
textView.setMovementMethod(LinkMovementMethod.getInstance())
textView.setText(Html.fromHtml(getString(R.string.link)))
  1. Do not add anything to the xml (like autoLink etc).
  2. Define link in strings.xml like

<string name="link"><![CDATA[<a href="https://google.com">Google link</a>]]></string>

时间你老了 2025-01-10 11:02:36

Android 不会自动使包含有效链接的字符串可点击。您可以做的是将自定义视图添加到对话框中并使用 WebView 来显示警报消息。在这种情况下,您可以将 html 存储在资源中,并且它们将是可点击的。

View alertDialogView = LayoutInflater.inflate(R.layout.alert_dialog_layout, null);

WebView myWebView = (WebView) alertDialogView.findViewById(R.id.dialogWebView);
myWebView.loadData("<a href=\"http://google.com\">Google!</a>", "text/html", "utf-8");
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setView(alertDialogView);

警报对话框布局.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<WebView android:id="@+id/dialogWebView" android:layout_height="wrap_content"
    android:layout_width="wrap_content" />

Android doesn't make strings that contain valid link clickable automatically. What you can do, is add custom view to your dialog and use WebView to show the alert message. In that case, you can store html in your resources and they will be clickable.

View alertDialogView = LayoutInflater.inflate(R.layout.alert_dialog_layout, null);

WebView myWebView = (WebView) alertDialogView.findViewById(R.id.dialogWebView);
myWebView.loadData("<a href=\"http://google.com\">Google!</a>", "text/html", "utf-8");
AlertDialog.Builder builder = new AlertDialog.Builder(MyActivity.this);
builder.setView(alertDialogView);

alert_dialog_layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<WebView android:id="@+id/dialogWebView" android:layout_height="wrap_content"
    android:layout_width="wrap_content" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文