【问题标题】:How can I get clickable hyperlinks in my AlertDialog in my Android app?如何在我的 Android 应用程序的 AlertDialog 中获得可点击的超链接?
【发布时间】:2016-06-01 14:44:55
【问题描述】:

还有另一个类似的问题,除了我试图避免使用 TextView,因为我没有伴随此错误消息的 XML 文件。当他点击超链接文本时,我试图将我的用户重定向到一个链接。这是我到目前为止的代码:

public void messageAlert(){
    String link = "www.google.com";
    new AlertDialog.Builder(this)
    .setTitle("ErrorMessage")
    .setMessage("You have a massive error. Please go to "+ link) // hyperlink here
    .setIcon(R.drawable.ic_action_error)
    .setPositiveButton("Continue", new DialogInterface.OnClickListener(){
            public void onClick(DialogInterface dialog, int n){

            }
    })
    .show();
}

任何帮助将不胜感激! 附言我是安卓初学者

【问题讨论】:

标签: android user-interface hyperlink custom-controls android-alertdialog


【解决方案1】:

您需要使用一个 href 这个链接上接受的答案显示了一个很好的例子。 a href example

TextView tv1 = new TextView(this);
tv1.setText(Html.fromHtml("<a href=\"" + "http:\\www.google.com" + "\">" + "Go to google" + "</a>"));
tv1.setClickable(true);
tv1.setMovementMethod(LinkMovementMethod.getInstance());

只需将此 TextView 添加到您的对话框中,它就会将 Go to google 显示为普通的超链接,单击该链接将打开浏览器并搜索 http:\www.google.com

【讨论】:

  • 如何将它添加到我的对话框中?
  • 您可以使用自己的布局 xml 文件 dialog.xml 创建自定义对话框,然后在其中添加 textview。 (developer.android.com/guide/topics/ui/dialogs.html) 在那里你找到一个例子,首先从你的布局文件中创建一个View view = inflater.inflate(R.layout.dialog, null); 使用它来找到文本视图TextView tv1 = (TextView) view.findViewById(R.id.textview); 并在这个builder.setView(view); 之后进行设置,其余的保持与示例中的相同。
  • `public void show() { LayoutInflater layoutInflater = LayoutInflater.from(mActivity);查看视图= layoutInflater.inflate(R.layout.dialog, null); TextView tv1 = (TextView) view.findViewById(R.id.textview); tv1.setText(Html.fromHtml("" + "去谷歌" + "")); tv1.setClickable(true); tv1.setMovementMethod(LinkMovementMethod.getInstance()); final AlertDialog.Builder dialogBu​​ilder = new AlertDialog.Builder(mActivity); dialogBu​​ilder.setView(view); AlertDialog 对话框 = dialogBu​​ilder.create();对话框.show(); }'
  • Whats mActivity 我没有对话
  • mActivity 是一个 Activity 对象,您必须在调用函数 show() 之前声明它。 Activity mActivity; 如果您的活动中有此功能,您可以将“mActivity”替换为“this”,这将显示一个对话框。您只需要在其中声明了 TextView 的 dialog.xml 文件。 id = textview。
猜你喜欢
  • 2015-03-13
  • 1970-01-01
  • 2020-08-06
  • 2011-01-01
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
相关资源
最近更新 更多