gpt4 book ai didi

java - 警报对话框中的 HTTP 链接不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:12:01 24 4
gpt4 key购买 nike

我尝试按照以下示例在我的警报对话框中的 TextView 中创建一个可点击的 HTTP 链接,但它不起作用:

How do I make links in a TextView clickable?

让它工作的唯一方法是将链接的文本直接放入我的字符串并将 android:autoLink="web" 放入我的 TextView。但我不想看到整个链接,只希望看到名称。

这里是创建对话框的地方:

@Override
protected Dialog onCreateDialog(int id) {
LayoutInflater factory = LayoutInflater.from(this);

switch (id) {
case DISCLAIMER:
final View disclaimerView = factory.inflate(R.layout.disclaimer_dialog, null);
final TextView dtv = (TextView) disclaimerView.findViewById(R.id.disclaimer_body);

dtv.setText("v"+versionName+"\n\n\u00A9"+Calendar.getInstance().get(Calendar.YEAR)+" "+"Developer\n\n" +
getString(R.string.alert_dialog_disclaimer_body));

dtv.setMovementMethod(LinkMovementMethod.getInstance());

return new AlertDialog.Builder(MyActivity.this)
.setView(disclaimerView)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
})
.create();
}

return null;
}

这是我位于 R.layout.disclaimer_dialog 中的 TextView:

<TextView
android:id="@+id/disclaimer_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="10dp"
android:gravity="center"
/>

这是我的字符串资源:

<string name="alert_dialog_disclaimer_body">This application was developed and written by me.\n\nGraphs use AChartEngine licensed under <a href="http://www.apache.org/licenses/LICENSE-2.0">Apache License 2.0</a>.\n\nThe rest goes here.</string>

最佳答案

TextView.setText(CharSequence) 的文档解释:

Sets the string value of the TextView. TextView does not accept HTML-like formatting, which you can do with text strings in XML resource files. To style your strings, attach android.text.style.* objects to a SpannableString, or see the Available Resource Types documentation for an example of setting formatted text in the XML resource file.

按照他们的建议,Formatting and Styling使用 HTML 标记设置样式标题下解释了如何同时拥有 HTML 标记和格式代码。

我认为在您的情况下,最简单的事情就是调整您的布局,将您的版权声明放在您已经使用的一个 View 中,并将免责声明放在下面的第二个 View 中,您只需单独放置:

<TextView
android:id="@+id/disclaimer_body"
android:text="@string/alert_dialog_disclaimer_body"
...
/>

关于java - 警报对话框中的 HTTP 链接不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11255473/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com