android textview 两种字体颜色,Android一个TextView去更改字体的不同颜色并设置不同的点击事件...

由于目前APP的上架需求,有的商城已经开始了免责声明,虽然不知道这样做是好是坏,但是需求在那我们就必须去实现了╮(╯▽╰)╭先看下效果图

1103744e2280

最终展现结果

在需求中我们需要将下面的文字放到一起,并更改不同的颜色进行区分,还需要将用《用户协议》和《隐私政策》添加不同的点击事情,去跳转进行展示不同的说明

首先这是一个弹框,我们需要先去定义它的布局文件

android:layout_width="250dp"

android:layout_height="wrap_content"

android:background="@drawable/bg_white_5dp"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center"

android:paddingTop="30dp"

android:text="用户协议和隐私政策"

android:textColor="@color/color_111111"

android:textSize="15sp" />

android:id="@+id/tv_content"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:gravity="center_vertical"

android:lineSpacingMultiplier="1.3"

android:paddingStart="15dp"

android:paddingTop="14dp"

android:paddingEnd="15dp"

android:paddingBottom="20dp"

android:text=""

android:textColor="@color/color_333333"

android:textSize="13sp" />

android:layout_width="match_parent"

android:layout_height="1dp"

android:background="#EEEEEE" />

android:layout_width="match_parent"

android:layout_height="50dp"

android:orientation="horizontal">

android:id="@+id/tv_cancel"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:gravity="center"

android:text="暂不使用"

android:textColor="@color/color_333333"

android:textSize="16sp" />

android:layout_width="1dp"

android:layout_height="match_parent"

android:background="#EEEEEE" />

android:id="@+id/tv_sure"

android:layout_width="0dp"

android:layout_height="match_parent"

android:layout_weight="1"

android:gravity="center"

android:text="同意"

android:textColor="@color/colorMain"

android:textSize="16sp" />

然后我们需要去定义一个条件去判断是否是第一次打开软件,如果是的话弹出我们这个摊款,如果不是的话则不弹出

//第一次打开APP弹出弹框

if (StringUtils.isEmpty(MyApplication.getIsNew())) {

showDialog();

}

接下来就是针对dialog的操作,直接上干货

private void showDialog() {

final Dialog mDialog;

mDialog = new Dialog(this, R.style.Teldialog);

mDialog.setContentView(R.layout.dialog_show);

mDialog.setCanceledOnTouchOutside(false);

mDialog.setCancelable(false);

mDialog.show();

TextView content = mDialog.findViewById(R.id.tv_content);

String str = "请您务必审慎阅读、充分理解“用户协议”和“隐私政策”各条款,包括但不限于:" +

"为了向您提供交易相关基本功能,我们会收集、使用必要的信息。你可阅读" +

"《用户协议》" + "和" +

"《隐私政策》" +

"了解详细信息。如您同意,请点击“同意”接受我们的服务。";

SpannableStringBuilder ssb = new SpannableStringBuilder();

ssb.append(str);

//第一个出现的位置

final int start = str.indexOf("《");

ssb.setSpan(new ClickableSpan() {

@Override

public void onClick(View widget) {

//用户服务协议点击事件

Bundle bundle = new Bundle();

bundle.putString("title", "用户协议");

bundle.putInt("showType", 0);

bundle.putString("content", MyApplication.getDataIndex().get("SYSUSER_PROTOL"));

go(WebActivity.class, bundle);

}

@Override

public void updateDrawState(TextPaint ds) {

super.updateDrawState(ds);

//设置文件颜色

ds.setColor(getResources().getColor(R.color.colorMain));

// 去掉下划线

ds.setUnderlineText(false);

}

}, start, start + 6, 0);

//最后一个出现的位置

final int end = str.lastIndexOf("《");

ssb.setSpan(new ClickableSpan() {

@Override

public void onClick(View widget) {

//隐私协议点击事件

Bundle bundle = new Bundle();

bundle.putString("title", "隐私政策");

bundle.putInt("showType", 0);

bundle.putString("content", MyApplication.getDataIndex().get("SYSUSER_HIDE_PROTOL"));

go(WebActivity.class, bundle);

}

@Override

public void updateDrawState(TextPaint ds) {

super.updateDrawState(ds);

//设置文件颜色

ds.setColor(getResources().getColor(R.color.colorMain));

// 去掉下划线

ds.setUnderlineText(false);

}

}, end, end + 6, 0);

content.setMovementMethod(LinkMovementMethod.getInstance());

content.setText(ssb, TextView.BufferType.SPANNABLE);

mDialog.findViewById(R.id.tv_cancel).setOnClickListener(v -> {

mDialog.dismiss();

finish();

});

mDialog.findViewById(R.id.tv_sure).setOnClickListener(v -> {

mDialog.dismiss();

//更改状态,同意下次进入软件则不再弹出弹框

MyApplication.setIsNew("not");

});

}

最后将dialog的样式附上

@color/windowTransaction

@null

true

true

bottom

true

true

@null

@android:style/Animation.Dialog

true

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值