Android EditText:如何禁用触摸时的光标移动? [英] Android EditText: How to disable cursor move on touch?

查看:1130
本文介绍了Android EditText:如何禁用触摸时的光标移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有EditText,它显示类似###-###的内容.我希望用户只能从第1个位置开始更改此文本.也就是说,用户不应在中间触摸#并进行更改.我怎样才能做到这一点?非常感谢.

I have EditText which displays something like ###-###. I want the user to be able to change this text only from the 1st position onward. That is user should not be able to touch a # in the middle and change it. How can I do this? Thanks a lot.

对不起,我的提问不够精确.我甚至想禁用文本中间的内容.

Sorry, I was not precise in my question. I want to disable even tapping in the middle of the text.

推荐答案

如果用户尝试轻按edittext来移动光标,则以下代码将强制光标停留在最后位置:

Following code will force the curser to stay in last position if the user tries to move it with a tap on the edittext:

edittext.setCursorVisible(false);

    edittext.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            edittext.setSelection(edittext.getText().length());
        }
    });

请注意,用户仍然可以通过箭头键和/或轨迹球更改光标的位置.据我所知,目前没有解决此问题的方法.

Note that the user can still change the position of the curser via arrow keys and / or trackball. As far as I know there is currently no workaround for this issue.

这篇关于Android EditText:如何禁用触摸时的光标移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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