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

标签 java android http textview android-alertdialog

我尝试按照以下示例在我的警报对话框中的 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/

相关文章:

java - iText 显示 unicode 字符的问题

java - Android ClassNotFoundException

Java:异常本身为空

java - 填充后列表为空,但微调器显示它有字符串

java - 为什么我从 Jakarta Commons HttpClient 收到空请求?

asp.net-mvc - ASP.NET Core 2.0 : HTTP requests timeout, 但 HTTPS 请求没有

java - 无法找到命名空间的 NamespaceHandler [http ://www. springframework.org/schema/context]

android - 已根据通知打开 Activity

android - 从 URI 旋转图像并将旋转后的图像保存到同一位置

ios - 在 iOS Swift 中将远程 JSON 数据同步到本地缓存存储