Android: 当 TextView 中的特定单词被点击时显示弹出对话框

7
在我的应用程序中,我有一些与条款和条件相关的文本。
引用: 注册即表示您同意受我们的使用条款约束,并已阅读我们的隐私政策。
“使用条款”和“隐私政策”这两个词应该是可点击的,并显示一个警报框。
首先,我尝试将句子分成四个TextView: 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();

问题在于四个TextView没有正确对齐。 这意味着我必须使用一个TextView,并且在TextView中单击特定单词(使用条款,隐私政策)时显示弹出式对话框。
如何最好地做到这一点?

我认为ClickableSpan会对我有所帮助。 - Sridhar
1个回答

11

我认为你需要使用 Spannable Strings。此外,这个问题会有所帮助。创建一个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);
   }

好建议,但通过编写一些代码来解释一下,使其成为一个好答案。 - Manoj Kumar
@ManojKumar:答案中的链接有代码。不过,代码已经被添加了。 - gsb
@Nerd 当用户点击“使用条款”时,我想显示terms.html页面,并且在同时显示privacy.html页面以展示隐私政策。 - Sridhar
URL会根据所点击的单词动态更改。 - Sridhar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接