android:autoLink=“phone”如何处理点击?
My layout.xml代码段:android:autoLink=“phone”如何处理点击?,android,Android,My layout.xml代码段: <TextView android:id="@+id/textViewPhoneValue" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoLink="phone" android:textColorLink="@color/color_blue" /> 嗯。现在电话显示为蓝色链接。当
<TextView
android:id="@+id/textViewPhoneValue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autoLink="phone"
android:textColorLink="@color/color_blue" />
嗯。现在电话显示为蓝色链接。当点击时,它会打开电话呼叫。很好
我使用android:autoLink=phone,因为许多电话号码的格式都是正确的。
现在,我需要处理用户单击此链接并使用自定义处理程序的时间。
textViewPhoneValue可以包含五个电话号码。我需要当点击第二个电话号码获得这个号码。有可能吗?更好的方法是使用多个textView,但如果您无法做到这一点。 可以使用可扩展文本添加多个单击功能
String s="Phone1 Phone2 Phone3";
SpannableString ss = new SpannableString(s);
String first =" Phone1";
String second =" Phone2";
String third =" Phone3";
int firstIndex = s.toString().indexOf(first);
int secondIndex = s.toString().indexOf(second);
ClickableSpan firstwordClick = new ClickableSpan() {
@Override
public void onClick(View widget) {
///............
}
};
ClickableSpan secondwordClick = new ClickableSpan() {
@Override
public void onClick(View widget) {
///............
}
};
ss.setSpan(firstwordClick,firstIndex, firstIndex+first.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
ss.setSpan(secondwordClick,secondIndex, secondIndex+second.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setLinksClickable(true);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(ss,BufferType.SPANNABLE);
这不是一个很好的例子,但你可以理解它的主要思想。@user8542613你可以删除autoLink,只需使用View.onClickListener来处理它就行了。见我上面的评论