如何在 AlertDialog 中获得可点击的超链接以导航到新的 Activity?

How can I get clickable hyperlinks in AlertDialog that will navigate to a new Activity?

我想做的是在 AlertDialog 显示的消息中有一个可点击的超链接。我的目标是弹出一个 AlertDialog,询问用户是否接受或拒绝类似于以下内容的条款和条件:

当点击 "Google Play Terms of Service" 超链接时,我想开始一个新的 Activity。我看过这里,我可以找到如何将超链接添加到网站以及如何在 TextView 中为文本添加超链接,但我无法为 AlertDialog 执行此操作。我正在寻找一种方法来设置对话框以将超链接文本链接到 Activity,或者获取显示消息的 TextView(我可以自己操作 TextView)。

我已尝试 (TextView) getDialog().findViewById(android.R.id.message) 获取消息 TextView 但此 returns 无效。

如有任何帮助,我们将不胜感激!

使用 AlertDialog.setView() 在消息区域显示包含超链接 TextView 的自定义布局。

例如制作一个 hyperlink_layout.xml 包含一个 TextView with id = hyperlink_text

只需包含AlertDialog消息区显示的内容即可,不需要buttons/title等

像这样创建 AlertDialog(来自 Activity):

LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.hyperlink_layout, null);

// get the text view from the view:
TextView hyperlinkText = (TextView)dialogView.findViewById(R.id.hyperlink_text);

// format/set it up how you like...

// handle the click on the hyperlink:
hyperlinkText.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // handle the click, launch the new Activity etc
        }
    });

// create the alert dialog
new AlertDialog.Builder(this)                       
    .setView(dialogView)
    .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // handle OK
        }
    })
    .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) { 
            // handle Cancel
        }
    })
    .show();

最好将此对话框创建为Activity,背景透明。