讲一下主要思路,通过手机号正则匹配循环找出所有的字符串,并循环通过SpannableString给目标字符串设置样式和点击事件
核心代码:
private void setNote(String note, TextView textView) {
if (TextUtils.isEmpty(note)) {
return;
}
SpannableString spannableString = new SpannableString(note);
String strNote = note;
String phoneNum = StringUtils.getPhoneNum(strNote);
while (!TextUtils.isEmpty(phoneNum)) {
setClick(spannableString, phoneNum, textView, note);
strNote = strNote.substring(strNote.indexOf(phoneNum) + 12);
phoneNum = StringUtils.getPhoneNum(strNote);
}
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
private void setClick(SpannableString spannableString, String phoneNum, TextView textView,
String note) {
spannableString.setSpan(new ClickableSpan() {
@Override public void updateDrawState(@NonNull TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(getResources().getColor(R.color.colorNumberMarkPress)); //设置电话号码字体颜色
ds.setUnderlineText(false); //设置电话号码下划线
}
@Override public void onClick(@NonNull View widget) {
//电话号码点击事件
outcallPop.show(phoneNum);
}
}, note.indexOf(phoneNum), note.indexOf(phoneNum) + 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(
new ForegroundColorSpan(getResources().getColor(R.color.colorNumberMarkPress)),
note.indexOf(phoneNum), note.indexOf(phoneNum) + 11, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
public class RecordOutcallDialogUtils {
@BindView(R.id.tvOutCall) TextView tvOutCall;
@BindView(R.id.tvCopyNum) TextView tvCopyNum;
@BindView(R.id.tvCancel) TextView tvCancel;
@BindView(R.id.tvIsNumberTip) TextView tvIsNumberTip;
@BindView(R.id.rlMain) RelativeLayout rlMain;
private Activity context;
private MyDialog outCallDialog;
private CallDlgClickListener dlgClickListener;
private String number;
public RecordOutcallDialogUtils(Activity context, CallDlgClickListener dlgClickListener) {
this.context = context;
this.dlgClickListener = dlgClickListener;
init();
}
private void init() {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.dlg_record_out_call, null);
outCallDialog = new MyDialog(context, R.style.DialogTheme);
ButterKnife.bind(this, view);
outCallDialog.setContentView(view);
tvIsNumberTip.setVisibility(View.GONE);
tvCancel.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
outCallDialog.dismiss();
if (dlgClickListener != null) {
dlgClickListener.onCancelClick();
}
}
});
rlMain.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
outCallDialog.dismiss();
}
});
tvCopyNum.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
outCallDialog.dismiss();
if (dlgClickListener != null) {
dlgClickListener.onCopyClick(number);
}
}
});
tvOutCall.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
outCallDialog.dismiss();
if (dlgClickListener != null) {
dlgClickListener.onCallClick(number);
}
}
});
Window window = outCallDialog.getWindow();
window.setWindowAnimations(R.style.bottom_dialog_animStyle);
window.setGravity(Gravity.BOTTOM);
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
WindowManager.LayoutParams attributes = window.getAttributes();
outCallDialog.setCancelable(true);
}
public void show(String number) {
this.number = number;
tvOutCall.setText(String.format(context.getString(R.string.str_call_num), number));
outCallDialog.show();
}
public boolean isShow() {
return outCallDialog != null && outCallDialog.isShowing();
}
public void dismiss() {
outCallDialog.dismiss();
}
public interface CallDlgClickListener {
void onCopyClick(String number);
void onCancelClick();
void onCallClick(String number);
}
}
private void initCallPop() {
outcallPop = new RecordOutcallDialogUtils(getActivity(),
new RecordOutcallDialogUtils.CallDlgClickListener() {
@Override public void onCopyClick(String number) {
SystemAboutUtils.setClipboard(getActivity(), "number", number);
ToastUtils.showShortToast("复制成功");
}
@Override public void onCancelClick() {
}
@Override public void onCallClick(String number) {
Intent intent = new Intent(getActivity(), DialNumActivity.class);
intent.putExtra(DialNumActivity.DIAL_NUMBER, number);
startActivity(intent);
}
});
}
♞无名小白白 发布了10 篇原创文章 · 获赞 4 · 访问量 985 私信 关注