textview可以设置监听么
展开全部
举例:TextView中存在“斯蒂芬斯蒂芬斯蒂芬1L斯蒂芬斯蒂芬斯蒂芬”这样的字段,我需要可以点击“1L”触发事件,做法如下:
TextView textView = (TextView) findViewById(R.id.cao2);
String str = "斯蒂芬斯蒂芬斯蒂芬1L斯蒂芬斯蒂芬斯蒂芬";
SpannableStringBuilder cao2 = new SpannableStringBuilder(str);
Pattern pattern2 = Pattern.compile("1L");
Matcher matcher2 = pattern2.matcher(str);
while(matcher2.find()) {
final String group = matcher2.group();
ClickableSpan what = new ClickableSpan() {
@Override
public void onClick(View widget) {
new AlertDialog.Builder(Spannable.this).setMessage(group).setPositiveButton("ok", null).show();
}
};
cao2.setSpan(what, matcher2.start(), matcher2.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textView.setText(cao2);
textView.setMovementMethod(LinkMovementMethod.getInstance());
TextView textView = (TextView) findViewById(R.id.cao2);
String str = "斯蒂芬斯蒂芬斯蒂芬1L斯蒂芬斯蒂芬斯蒂芬";
SpannableStringBuilder cao2 = new SpannableStringBuilder(str);
Pattern pattern2 = Pattern.compile("1L");
Matcher matcher2 = pattern2.matcher(str);
while(matcher2.find()) {
final String group = matcher2.group();
ClickableSpan what = new ClickableSpan() {
@Override
public void onClick(View widget) {
new AlertDialog.Builder(Spannable.this).setMessage(group).setPositiveButton("ok", null).show();
}
};
cao2.setSpan(what, matcher2.start(), matcher2.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
textView.setText(cao2);
textView.setMovementMethod(LinkMovementMethod.getInstance());
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询