【问题标题】:Make a link clickable in AlertDialog window in Android App在 Android App 的 AlertDialog 窗口中使链接可点击
【发布时间】:2016-09-10 15:18:05
【问题描述】:

我需要在 onPostExecute 方法打开的 AlertDialog 窗口中打开一个链接(仅当我点击它时)(例如:https://en.wikipedia.org/wiki/Quantico_(TV_series)):

@Override
protected void onPostExecute(String result) {
    String res=result.replace("^", "\n");
    String link=result.substring(result.indexOf("[") + 1, result.indexOf("]"));
    res=res.replaceAll("[.*?]", "");
    res=res.replace("[", "");
    res=res.replace("]","");
    alertDialog.setMessage(res+link);
    alertDialog.show();
    //System.out.println("RESULT FOUND ===>> "+ res);
    //UtilityFunct.list.clear();
}

这是我的 Nexus 5X 上的输出示例:

Output on my phone

我已使用“替换”来隔离我需要使其可点击的链接。 我尝试了很多在 stackoverflow 中找到的方法,但没有一种方法有效。

我该怎么办?可能吗? 谢谢

【问题讨论】:

    标签: android hyperlink android-alertdialog clickable


    【解决方案1】:

    试试下面的代码

    TextView message = new TextView(YourClass.this);
    SpannableString s = new SpannableString("Text with like http://yoursite.com"");
    Linkify.addLinks(s, Linkify.WEB_URLS);
    message.setText(s);
    message.setMovementMethod(LinkMovementMethod.getInstance());
    alertDialog.setView(message);
    alertDialog.show();
    

    【讨论】:

    • 感谢您的回答。但我不能让它工作。我在“YourClass.this”中有一个错误。我已经尝试过我项目的每一类(String、AlertDialog、BackgroundTask...),但都不起作用
    • 你只检查过(这个)吗?
    • 是的,不起作用。也许问题是在 AlertDialog 中使用 TextView?
    • 你放置 onPostExecute 的类的名称是什么?
    • 类的名称是“BackgroundTask”,我们用它来建立与 PHP 脚本的通信。传递给 onPost 的变量“String result”源自在数据库上工作的 PHP 脚本。会不会是问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 2012-03-13
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多