android 自定义 URLSpan 无效
android custom URLSpan not working
问题是在单击 URL 跨度时处理我自己的操作。我写了自定义 URLSpan 但它不起作用。
这是我的习惯URL跨度:
public class CustomURLSpan extends android.text.style.URLSpan {
private Command mClickAction;
public CustomURLSpan(String url, Command clickAction) {
super(url);
mClickAction = clickAction;
}
@Override
public void onClick(View widget) {
try {
mClickAction.execute();
} catch (Exception e) {
}
}
public static void clickifyTextView(TextView tv, Command clickAction) {
SpannableString current = new SpannableString(tv.getText());
URLSpan[] spans =
current.getSpans(0, current.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = current.getSpanStart(span);
int end = current.getSpanEnd(span);
current.removeSpan(span);
current.setSpan(new CustomURLSpan(span.getURL(), clickAction), start, end, 0);
}
}
public interface Command {
void execute();
}
}
我在这里使用它:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle bundle = getArguments();
String message = bundle.getString("message");
final Activity activity = getActivity();
text = new TextView(activity);
text.setText(message);
Linkify.addLinks(text, Linkify.EMAIL_ADDRESSES);
CustomURLSpan.clickifyTextView(text, new CustomURLSpan.Command() {
@Override
public void execute() {
//I want to do my stuff here, but not working
}
});
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setView(text);
alertDialogBuilder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
...
}
但是如果我点击 url,我会看到原生的 android 对话框来选择电子邮件程序。我在网上找到的所有例子都是一样的。
编辑: 根据@CommonWare 的回答。我只需要:
...
public static void clickifyTextView(TextView tv, Command clickAction) {
SpannableString current = new SpannableString(tv.getText());
URLSpan[] spans =
current.getSpans(0, current.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = current.getSpanStart(span);
int end = current.getSpanEnd(span);
current.removeSpan(span);
current.setSpan(new CustomURLSpan(span.getURL(), clickAction), start, end, 0);
tv.setText(current); //this is what I need
}
}
public interface Command {
void execute();
}
clickifyTextView()
从 TextView
中检索文本,将其包装在新的 SpannableString
中...然后永远不会更新 TextView
。所以 clickifyTextView()
正在修改 TextView
中内容的副本,因此不会影响 TextView
.
尝试在 clickifyTextView()
中的跨度转换循环之后在 TextView
上调用 setText()
。
在使用任何 ClickableSpan
时将 movementMethod = LinkMovementMethod()
设置为您的文本视图
问题是在单击 URL 跨度时处理我自己的操作。我写了自定义 URLSpan 但它不起作用。
这是我的习惯URL跨度:
public class CustomURLSpan extends android.text.style.URLSpan {
private Command mClickAction;
public CustomURLSpan(String url, Command clickAction) {
super(url);
mClickAction = clickAction;
}
@Override
public void onClick(View widget) {
try {
mClickAction.execute();
} catch (Exception e) {
}
}
public static void clickifyTextView(TextView tv, Command clickAction) {
SpannableString current = new SpannableString(tv.getText());
URLSpan[] spans =
current.getSpans(0, current.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = current.getSpanStart(span);
int end = current.getSpanEnd(span);
current.removeSpan(span);
current.setSpan(new CustomURLSpan(span.getURL(), clickAction), start, end, 0);
}
}
public interface Command {
void execute();
}
}
我在这里使用它:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Bundle bundle = getArguments();
String message = bundle.getString("message");
final Activity activity = getActivity();
text = new TextView(activity);
text.setText(message);
Linkify.addLinks(text, Linkify.EMAIL_ADDRESSES);
CustomURLSpan.clickifyTextView(text, new CustomURLSpan.Command() {
@Override
public void execute() {
//I want to do my stuff here, but not working
}
});
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setView(text);
alertDialogBuilder.setNegativeButton("Close", new DialogInterface.OnClickListener() {
...
}
但是如果我点击 url,我会看到原生的 android 对话框来选择电子邮件程序。我在网上找到的所有例子都是一样的。
编辑: 根据@CommonWare 的回答。我只需要:
...
public static void clickifyTextView(TextView tv, Command clickAction) {
SpannableString current = new SpannableString(tv.getText());
URLSpan[] spans =
current.getSpans(0, current.length(), URLSpan.class);
for (URLSpan span : spans) {
int start = current.getSpanStart(span);
int end = current.getSpanEnd(span);
current.removeSpan(span);
current.setSpan(new CustomURLSpan(span.getURL(), clickAction), start, end, 0);
tv.setText(current); //this is what I need
}
}
public interface Command {
void execute();
}
clickifyTextView()
从 TextView
中检索文本,将其包装在新的 SpannableString
中...然后永远不会更新 TextView
。所以 clickifyTextView()
正在修改 TextView
中内容的副本,因此不会影响 TextView
.
尝试在 clickifyTextView()
中的跨度转换循环之后在 TextView
上调用 setText()
。
在使用任何 ClickableSpan
movementMethod = LinkMovementMethod()
设置为您的文本视图