【问题标题】:Positioning the cursor in an un-editable EditText on Android application将光标定位在 Android 应用程序上不可编辑的 EditText 中
【发布时间】:2017-12-28 07:53:24
【问题描述】:

我一直在阅读 How to set cursor position in EditText? 帖子中的答案,我的 EditText 中确实有一个特定问题,这可能是问题的原因。在我的 xml 布局中,我定义了一个名为“edit”的 EditText,它的 inputType = none。根据 android 开发者文档,如果 inputType = none,则文本不可编辑。 “无”的原因是我防止 android 通用键盘在我的 User-Interface 上弹出的方式,效果很好。 我想做这样的事情:用户输入:5 + sin(),现在光标editText光标位置应该在)符号之后,但是由于inputType = none,所以没有显示光标。我需要做的是移动下一个文本输入将放置在 ( ) 符号之间的位置。发生的情况是下一个文本输入紧跟在 ) 符号之后。 所以如果下一个文本输入是 4,那么: 5 + sin( )4 ,我不想要这个。我宁愿有以下几点:5 + sin(4 )

我正在使用带有“( )”按钮的定制键盘。这是我尝试过但没有成功的方法:

//once the user presses the "( )" button:
Selection.setSelection(edit.getText(),edit.getText().length() - 2);

我尝试过的另一种方法:

//once the user presses the "( )" button:
edit.setInputType(InputType.TYPE_CLASS_NUMBER);
edit.setSelection(edit.getText().length() - 2);
edit.setInputType(InputType.TYPE_NULL);

这两种方法都不适合我。我还尝试将editText的inputType设置为xml文件上的“none”之外的其他内容。但是,android 键盘弹出在我定制的键盘之上。

任何想法或想法都会有所帮助

【问题讨论】:

    标签: android android-edittext uitextposition


    【解决方案1】:

    我不知道你的 () 按钮是如何工作的,但你可以试试这个。

    yourEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
                if( s.length()>0 && s.charAt(s.length()-1) == ')' ){
                yourEditText.setSelection(s.length()-1);
                }
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
    
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-13
      • 1970-01-01
      • 2011-12-10
      • 1970-01-01
      相关资源
      最近更新 更多