如何在.setMessage Android代码中的Alertdialog中包含链接URL

时间:2015-09-21 15:18:01

标签: android cookies

它正在添加指向存储在外部网址中的Cookie政策的链接。但不是在文本中添加链接。请帮我在文本中添加URL到策略URL。感谢

@Override
  public void onStart() {
    super.onStart();
    final SharedPreferences settings =
        getSharedPreferences("localPreferences", MODE_PRIVATE);
    if (settings.getBoolean("isFirstRun", true)) {
      new AlertDialog.Builder(this)
        .setTitle("Cookies")
        .setMessage("On this site device identifiers are used to personalize content and ads,"
                + " in order to provide social media features and to analyze traffic. "
                + "We also share these identifiers and other information on your device with our social media partners, "
                + "advertising and web analytics./// En este sitio se usan identificadores de dispositivo"
                + " para personalizar el contenido y los anuncios, con el fin de ofrecer funciones de medios sociales"
                + " y para analizar el tráfico. Además, compartimos estos identificadores y otra información"
                + " sobre su dispositivo con nuestros partners de medios sociales,"
                + " de publicidad y de análisis web. http://www.urlaboutcookiepolicy.com")
        .setNeutralButton("Close message", new OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
            settings.edit().putBoolean("isFirstRun", false).commit();
          }
        }).show();

    }
  }

2 个答案:

答案 0 :(得分:0)

我不确定你在问什么,但我假设你希望这个URL是一个超链接。

您将需要定义一个自定义布局对话框:http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout

然后您需要在布局中添加android:autoLink="web"

最后,用标签覆盖你的超链接:

<a href="http://www.urlaboutcookiepolicy.com">http://www.urlaboutcookiepolicy.com</a>

答案 1 :(得分:0)

使用以下代码在对话框中创建超链接。

TextView text=new TextView(this);
                text.setText(Html.fromHtml("<ahref=\"http://www.urlaboutcookiepolicy.com \">http://www.urlaboutcookiepolicy.com </a>"));
                text.setMovementMethod(LinkMovementMethod.getInstance());
                new AlertDialog.Builder(this)
                .setTitle("Cookies")
                .setView(text)
                .setNeutralButton("Ok",new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                }).show();