|
|
|
@ -214,7 +214,7 @@ public class NoteEditText extends EditText {
|
|
|
|
|
// 未被处理的按键事件交由父类处理
|
|
|
|
|
return super.onKeyUp(keyCode, event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
|
|
|
|
|
if (mOnTextViewChangeListener != null) {
|
|
|
|
@ -227,39 +227,57 @@ public class NoteEditText extends EditText {
|
|
|
|
|
super.onFocusChanged(focused, direction, previouslyFocusedRect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建上下文菜单(长按文本时弹出的菜单)
|
|
|
|
|
* 主要处理文本中的URL链接,为链接添加对应的菜单项
|
|
|
|
|
*/
|
|
|
|
|
@Override
|
|
|
|
|
protected void onCreateContextMenu(ContextMenu menu) {
|
|
|
|
|
// 检查当前文本是否包含富文本样式(如超链接)
|
|
|
|
|
if (getText() instanceof Spanned) {
|
|
|
|
|
// 获取当前选中的文本范围
|
|
|
|
|
int selStart = getSelectionStart();
|
|
|
|
|
int selEnd = getSelectionEnd();
|
|
|
|
|
|
|
|
|
|
// 确保起始和结束位置正确排序
|
|
|
|
|
int min = Math.min(selStart, selEnd);
|
|
|
|
|
int max = Math.max(selStart, selEnd);
|
|
|
|
|
|
|
|
|
|
// 从选中文本中提取URLSpan对象(超链接)
|
|
|
|
|
final URLSpan[] urls = ((Spanned) getText()).getSpans(min, max, URLSpan.class);
|
|
|
|
|
|
|
|
|
|
// 如果只选中了一个URL链接
|
|
|
|
|
if (urls.length == 1) {
|
|
|
|
|
int defaultResId = 0;
|
|
|
|
|
for(String schema: sSchemaActionResMap.keySet()) {
|
|
|
|
|
int defaultResId = 0; // 默认菜单项资源ID
|
|
|
|
|
|
|
|
|
|
// 遍历预定义的URL协议映射表
|
|
|
|
|
for(String schema : sSchemaActionResMap.keySet()) {
|
|
|
|
|
// 检查当前URL是否包含特定协议(如http, mailto等)
|
|
|
|
|
if(urls[0].getURL().indexOf(schema) >= 0) {
|
|
|
|
|
// 获取对应的菜单项文本资源ID
|
|
|
|
|
defaultResId = sSchemaActionResMap.get(schema);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 如果没有匹配的协议,使用默认"其他链接"菜单项
|
|
|
|
|
if (defaultResId == 0) {
|
|
|
|
|
defaultResId = R.string.note_link_other;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 添加菜单项并设置点击监听器
|
|
|
|
|
menu.add(0, 0, 0, defaultResId).setOnMenuItemClickListener(
|
|
|
|
|
new OnMenuItemClickListener() {
|
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {
|
|
|
|
|
// goto a new intent
|
|
|
|
|
// 当菜单项被点击时,触发URL链接的默认行为
|
|
|
|
|
// 比如打开浏览器或邮件客户端
|
|
|
|
|
urls[0].onClick(NoteEditText.this);
|
|
|
|
|
return true;
|
|
|
|
|
return true; // 表示事件已处理
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 调用父类方法继续处理其他上下文菜单逻辑
|
|
|
|
|
super.onCreateContextMenu(menu);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|