【问题标题】:add link to alertdialog text android [duplicate]添加链接到警报对话框文本android [重复]
【发布时间】:2015-03-04 10:36:59
【问题描述】:

我正在创建一个警报对话框,我在消息中放置了一个字符串。该字符串带有一些我希望将它们链接起来的文本,因此当它们显示在对话框中时,用户可以单击它们并转到其他视图。让我给你看一个我想要的例子:

public class Activity extends FragmentActivity{

(...)

    public void myMethod(){
        String message = "Some links: link1, link2, link3";
        AlertDialog.Builder builder = new AlertDialog.Builder(this)
        builder.setTitle("This is a title");
        builder.setMessage(message);
        builder.setCancelable(true);
        builder.setOnCancelListener(onCancelListener);
        builder.create().show();
    }
}

我想对链接 1、链接 2 和链接 3 做一些事情,所以当它们显示在对话框中时,它们会显示为链接。然后,当用户点击 link1 时,会显示一个新活动;当点击link2时,会显示另一个视图,与link3相同。

我想要一个 onlinkclicklistener 或类似的东西,以便在用户点击链接时捕获。

关于如何做到这一点的任何想法? 我希望你能明白我想要什么:)

【问题讨论】:

  • 您所说的视图是指要显示的新活动还是 html 页面
  • 尝试使用Html.fromHtml:builder.setMessage(Html.fromHtml(message));
  • @akaElement 我的意思是像一个新的活动,很抱歉的混淆。我想要一个 onlinkclicklistener 或类似的东西,所以当用户点击链接时我会捕捉到
  • 那么您应该为此目的使用自定义对话框

标签: android hyperlink android-alertdialog


【解决方案1】:
public void myMethod(){
        String link1 = "<a href=\"http://www.google.com\">http://www.google.com</a>";
        String message = "Some links: "+link1+"link1, link2, link3";
        Spanned myMessage = Html.fromHtml(message);


        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("This is a title");
        builder.setMessage(myMessage);
        builder.setCancelable(true);
        AlertDialog alertDialog = builder.create();
        alertDialog.show();
        TextView msgTxt = (TextView) alertDialog.findViewById(android.R.id.message);
        msgTxt.setMovementMethod(LinkMovementMethod.getInstance());
    }

【讨论】:

  • 这很好用。如果我将您的链接 1 更改为 ""aName";",当我点击“aName”(显示为链接)时,是否有可能我捕获该点击而不是打开“chrome”或“Internet”选项? (好吧,实际上这些选项只有在我实际使用链接而不是“aName”时才会出现,因为如果我使用“aName”,应用程序会崩溃,因为它不是 http 链接)
【解决方案2】:

正如我在评论中所说,您应该使用自定义对话框。

所以为自定义对话框的布局创建一个新的 xml 文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button1"
        android:text="firstButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        />

    <Button
        android:id="@+id/button2"
        android:text="SecondButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

以上将是您自定义对话框的布局。

现在在您使用AlertDialog.Builder 的方法中使用以下代码。

public void myMethod(){
final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.custome);
                dialog.setTitle("Title...");

                // set the custom dialog components - text, image and button
                Button button1 = (Button) dialog.findViewById(R.id.button1);
                button1.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(getBaseContext(),MyActivity2.class);
                        startActivity(intent);
                    }
                });

                Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                // if button is clicked, close the custom dialog
                dialogButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
            }
}

因此,这将显示您的自定义对话框,其布局在上述 xml 中指定,当您单击 button1 时,它将在新视图中打开 MyActivity2。

更多详情请查看here

【讨论】:

  • 当然,这是一个非常好的答案,并且经过一些更改,这将起作用。我会寻找其他更简单的建议,但如果我没有找到,我会将其标记为答案:)
  • @RedDeath 没有问题的朋友,但请检查答案中给出的链接。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-03
  • 2014-11-23
  • 1970-01-01
  • 2020-12-08
  • 1970-01-01
相关资源
最近更新 更多