处理点击隐私政策,后边空白区域也触发了点击事件

最近公司在用户登录界面要加《用户协议》以及《隐私政策》,话还没等产品说完,我就想到了用 SpannableString 做,说起来简单,当然,做起来也简单,之前做app都会有这种需求,但是这次字比较多,一行展示不完,如图1,2,不过这没关系,然后我就提给测试测了,过了会,刚要伸懒腰,测试过来说有问题,为啥点空白区域也会进隐私政策的界面,很纳闷,我测的时候没问题啊(原来是只点了字的,尴尬),那就改呗,于是踏上英雄的征途。

图片
图1
图片
图2

英雄的征途大致分为四个阶段

第一阶段:遇到问题;

第二阶段:寻找问题;

第三阶段:解决问题;

第四阶段:把这个解决问题方法分享给大家

第一阶段:遇到问题 ,测试人员提出了bug

第二阶段:寻找问题

通过SpannableString 虽然设置好了,但是还差点击事件的响应,如果不加下面这行代码,那么点击事件是没有用的,所以问题出现在了这。

  那就看源码吧,LinkMovementMethod类,点击事件肯定通过onTouchEvent方法实现,下图圈起来的,在特定的条件下会计算出错导致的,这里的特定条件有两种情况:

第一种:当你的TextView宽度设置为android:layout_width="wrap_content"并且存在换行时;

第二种:当你的TextView宽度设置为android:layout_width="match_parent"并且没有填满TextView时

第三阶段:解决问题;

  试过几个解决方法后,有个最简单的就是在字符串最后加入空格,如下:

   <string name="privacy_policy">《隐私政策》&#160;</string>

第四阶段:处理完成。

以下是全部代码:方便大家搬砖愉快。

 1、定义名字

    <string name="login_consent">登录注册即表明您已阅读并同意</string>
    <string name="user_service">《用户服务协议》&#160;</string>
    <string name="privacy_policy">《隐私政策》&#160;</string>

2、xml布局

 <TextView
            android:id="@+id/tv_agreement"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginBottom="20dp"
            />

3、登录协议工具类

public class LoginAgrementView {


    private Activity activity;
    private TextView tvArrgement;

    public LoginAgrementView(Activity activity, TextView tvArrgement) {
        this.activity = activity;
        this.tvArrgement = tvArrgement;
    }

    /**
     * 用户协议
     */
    public void useAgrement() {
        tvArrgement.setText(activity.getString(R.string.login_consent));
        SpannableString clickString = new SpannableString(activity.getString(R.string.user_service));
        clickString.setSpan(new ForegroundColorSpan(Color.parseColor("#FFFFFF")), 0, clickString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        clickString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                Log.e("---> $","用户协议");        }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(activity.getResources().getColor(R.color.FF1A65EE));
                ds.setUnderlineText(false);
            }
        }, 0, clickString.length()-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvArrgement.append(clickString);
        tvArrgement.append(new SpannableString("和"));
        SpannableString clickStringPrivacy= new SpannableString(activity.getString(R.string.privacy_policy));
        clickStringPrivacy.setSpan(new ClickableSpan() {
            @Override
            public void onClick(View widget) {
                 Log.e("---> $","隐私政策");
                   }

            @Override
            public void updateDrawState(TextPaint ds) {
                super.updateDrawState(ds);
                ds.setColor(activity.getResources().getColor(R.color.FF1A65EE));
                ds.setUnderlineText(false);
            }
        }, 0, clickStringPrivacy.length()-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvArrgement.append(clickStringPrivacy);
        //开始响应点击事件
        tvArrgement.setMovementMethod(LinkMovementMethod.getInstance());
    }

}

4、代码里引用

   LoginAgrementView loginAgrementView = new LoginAgrementView(this, binding.tvAgreement);
   loginAgrementView.useAgrement();

注意:因为加了空格,所以别忘了减1

 
 
 

猜你喜欢

转载自blog.csdn.net/qq_40533422/article/details/108662721