@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView textView = (TextView) view.findViewById(R.id.textview);
String str = "Click me!";
String txt = str + "Hello world!";
SpannableString spannableString = new SpannableString(txt);
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
//Do something.
if(isAdded()) {
Toast.makeText(getActivity(), "You have clicked!", Toast.LENGTH_LONG).show();
// avoidHintColor(widget);
}
}
@Override
public void updateDrawState(@NonNull TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(getResources().getColor(android.R.color.holo_red_dark));
ds.setUnderlineText(false);
ds.clearShadowLayer();
}
};
spannableString.setSpan(clickableSpan,0,str.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
textView.setMovementMethod(LinkMovementMethod.getInstance());
}
这样做好后发现点击设置无效果二设置的颜色却有效果,然后把代码如下改正后,可以了
tv_reap_null = new TextView(context);
tv_reap_null.setGravity(Gravity.CENTER);
tv_reap_null.setPadding(0, 50, 0, 0);
tv_reap_null.setTextColor(Color.parseColor("#676767"));
tv_reap_null.setTextSize(15);
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, 500);
tv_reap_null.setLayoutParams(params);
SpannableString string = new SpannableString(“dddddddddddddddddd”);
string.setSpan(new Click(), 10, 12, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
tv_reap_null.setText(string);
tv_reap_null.setMovementMethod(LinkMovementMethod.getInstance());//设置点击监听
mainView.addView(tv_reap_null);