首页 > 代码库 > 点击Textview url弹出ContextMenu
点击Textview url弹出ContextMenu
代码:
private static final String SCHEME_TEL = "tel:"; private static final String SCHEME_HTTP = "http:"; private static final String SCHEME_EMAIL = "mailto:"; private static final Map<String, Integer> sSchemaActionResMap = new HashMap<String, Integer>(); static{ sSchemaActionResMap.put(SCHEME_TEL, R.string.note_link_tel); sSchemaActionResMap.put(SCHEME_HTTP, R.string.note_link_web); sSchemaActionResMap.put(SCHEME_EMAIL, R.string.note_link_email); } @Override protected void onCreateContextMenu(ContextMenu menu) { // TODO Auto-generated method stub if (getText() instanceof Spanned) { int selStart = getSelectionStart(); int selEnd = getSelectionEnd(); int min = Math.min(selStart, selEnd); int max = Math.max(selStart, selEnd); final URLSpan[] urls = ((Spanned) getText()).getSpans(min, max, URLSpan.class);//start == end length=1; if (urls.length == 1) { int defaultResId = 0; for(String schema: sSchemaActionResMap.keySet()) { if(urls[0].getURL().indexOf(schema) >= 0) { 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 urls[0].onClick(XNoteEditText.this); return true; } }); } } super.onCreateContextMenu(menu); }
如图所示:
点击Textview url弹出ContextMenu
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。