根据用户单击在编辑文本中设置光标位置 [英] Set cursor position in edittext according to user click

查看:60
本文介绍了根据用户单击在编辑文本中设置光标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望光标跟随用户单击以编辑editText中的现有文本.要在平板电脑中禁用编辑操作栏,请使用:

I want the cursor to follow user click to edit existing text in the editText. To disable action bar of editing in tablet i use:

 edittext.setCustomSelectionActionModeCallback(new ActionMode.Callback() {

            public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            public void onDestroyActionMode(ActionMode mode) {
            }

            public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                return false;
            }

            public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                return false;
            }
        });

如何根据用户单击文本来获取光标位置?

How to get cursor position according to user click in text ?

推荐答案

Layout layout = ((EditText) v).getLayout();
float x = event.getX() + edittext.getScrollX();
int offset = layout.getOffsetForHorizontal(0, x); 
edittext.setSelection(offset); 

这篇关于根据用户单击在编辑文本中设置光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆