
我是android编程的新手.我为edittext添加了一个上下文菜单.我希望我可以长时间按下光标.请帮忙.我可以通过以下代码获取所选文本.
@OverrIDepublic voID onCreateContextMenu(ContextMenu menu, VIEw v, ContextMenuInfo menuInfo) { EditText edittext = (EditText)findVIEwByID(R.ID.editText1); menu.setheaderTitle(edittext.getText().toString().substring(edittext.getSelectionStart(), edittext.getSelectionEnd())); menu.add("copy");}edittext有一些文字,例如“一些文字.多一些文字”.用户单击“更多”,因此光标将位于单词“more”中的某些位置.我想当用户长按这个词时我可以得到“更多”这个词等等光标下的其他词.
解决方法:
EditText et = (EditText) findVIEwByID(R.ID.xx);int startSelection = et.getSelectionStart();String selecteDWord = "";int length = 0;for(String currentWord : et.getText().toString().split(" ")) { System.out.println(currentWord); length = length + currentWord.length() + 1; if(length > startSelection) { selecteDWord = currentWord; break; }}System.out.println("Selected word is: " + selecteDWord); 总结 以上是内存溢出为你收集整理的android – 从当前光标位置的EditText获取单词全部内容,希望文章能够帮你解决android – 从当前光标位置的EditText获取单词所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)