【问题标题】:Android: Display a pop-up dialog when a particular word in a TextView is clickedAndroid:单击 TextView 中的特定单词时显示弹出对话框
【发布时间】:2012-11-07 03:15:02
【问题描述】:

在我的申请中,我有一些条款和条件的文本。

注册即表示您同意遵守我们的使用条款并已阅读我们的隐私政策。

使用条款隐私政策应该是可点击的,并显示一个警告框。

首先我尝试将句子分成四个TextViews:

  1. 注册即表示您同意受我们的约束
  2. 使用条款
  3. 并且您已阅读我们的
  4. 隐私政策。

效果很好,使用以下布局 XML:

 <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/termsandcondtion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:orientation="horizontal" >

        <TextView
            android:id="@+id/terms_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/terms_1"
            android:textColor="#000000"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/terms"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:onClick="showTerms"
            android:text="@string/terms_2"
            android:textColor="#000000"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/terms_3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/terms_3"
            android:textColor="#000000"
            android:textSize="12dp" />

        <TextView
            android:id="@+id/termsofuse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:onClick="showTermsofUse"
            android:text="@string/terms_4"
            android:textColor="#000000"
            android:textSize="12dp" />
    </LinearLayout>

显示弹窗的代码是:

WebView wv = new WebView(this);

    wv.loadUrl(url);

    wv.setWebViewClient(new WebViewClient()
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);

            return true;
        }
    });
    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Privacy Policy");
    alert.setView(wv);
    alert.setNegativeButton("Close", new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface dialog, int id)
        {
        }
    });
    alert.show();

问题是四个TextViews 没有正确对齐。 这意味着我必须使用一个TextView 并在单击TextView 中的特定词(使用条款、隐私政策)时显示一个弹出对话框。

最好的方法是什么?

【问题讨论】:

  • 我认为 ClickableSpan 会帮助我..

标签: android android-layout android-widget textview


【解决方案1】:

我想你需要Spannable Strings。此外,this 问题会有所帮助。创建ClickableSpan 将是您解决问题的好选择。

SpannableString stringToSpan = new SpannableString("<your sentence>");
TextView textView = (TextView) findViewById(R.id.<yourTextView ID>);
textView.setText(stringToSpan);
textView.setMovementMethod(LinkMovementMethod.getInstance());
ClickableSpan clickableSpan = new ClickableSpan() {
      @Override
      public void onClick(View textView) {
       //alert box code here
       }
      };
stringToSpan.setSpan(clickableSpan, <start position of span>,<end position>,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
   }

【讨论】:

  • 好建议,但通过在代码中稍微解释一下,让它成为一个好的答案
  • @ManojKumar:答案中的链接有代码。不过,代码已添加。
  • @Nerd 我想在点击使用条款时显示 terms.html,同时在隐私政策中显示 privacy.html
  • URL 被点击的单词动态改变
猜你喜欢
  • 2018-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-20
  • 2011-10-14
相关资源
最近更新 更多