点击Textview url弹出ContextMenu
时间:2014-08-25 13:32:04
收藏:0
阅读:240
代码:
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); }
如图所示:
原文:http://my.oschina.net/u/1781028/blog/306246
评论(0)