如何禁用光标定位和文本选择在一个EditText? (安卓) [英] How to disable cursor positioning and text selection in an EditText? (Android)

查看:518
本文介绍了如何禁用光标定位和文本选择在一个EditText? (安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种方式来prevent从任何地方移动光标位置的用户。光标应该始终保持在当前的EditText值的末尾。除了该用户不应该能够以选择的EditText任何东西。你有什么想法如何用一个EditText认识到,在Android的?

I'm searching for a way to prevent the user from moving the cursor position anywhere. The cursor should always stay at the end of the current EditText value. In addition to that the user should not be able to select anything in the EditText. Do you have any idea how to realize that in Android using an EditText?

要澄清:用户应能够插入文字,但只有在末端

To clarify: the user should be able to insert text, but only at the end.

推荐答案

我有同样的问题。这结束了工作对我来说:

I had the same problem. This ended up working for me:

public class CustomEditText extends EditText {

    @Override
    public void onSelectionChanged(int start, int end) {

        CharSequence text = getText();
        if (text != null) {
            if (start != text.length() || end != text.length()) {
                setSelection(text.length(), text.length());
                return;
            }
        }

        super.onSelectionChanged(start, end);
    }

}

这篇关于如何禁用光标定位和文本选择在一个EditText? (安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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