微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

By registering you agree to be bound by our Terms of Use and that you have read our Privacy Policy.

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

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

>通过注册,您同意受我们的约束
>使用条款
>你已经读过我们了
>隐私政策.

这很好用,具有以下布局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并显示弹出式diaplog.

做这个的最好方式是什么?

解决方法

我想你需要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);
   }

原文地址:https://www.jb51.cc/android/309912.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐