【问题标题】:Cursor Not Moving Inside EditText for Custom Keyboard Input in Android光标不在 Android 中自定义键盘输入的 EditText 内移动
【发布时间】:2014-06-08 01:52:00
【问题描述】:

我通过以下方式禁用了软键盘:

   InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
 imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

对于 EditText 视图。正如我所料,键盘被禁用。我正在使用自己的自定义键盘输入值。我能够在第一个或最后一个位置或第一次在指定位置显示光标。

但是我在将光标移动到我按下的位置时遇到了问题,这意味着我无法在我在 EditText 中键入的文本内移动光标。 是否有任何解决方案可以将光标移动到自定义键盘输入的文本内。 提前致谢。

【问题讨论】:

    标签: android android-edittext android-cursor


    【解决方案1】:

    您可以使用editText1.setSelection(position) 来设置光标的位置。 您可以使用TextWatcher 类并获取长度并放置您的光标位置。

    ((EditText)findViewById(R.id.searchBox1)).addTextChangedListener(new TextWatcher() {
    
        public void onTextChanged(CharSequence s, int start, int before, int count) {
    
            String searchString = s.toString();
            int textLength = searchString.length();
            editText1.setSelection(textLength);         
        }
    
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }
    
        public void afterTextChanged(Editable s) {
        }
    });
    

    【讨论】:

      【解决方案2】:

      也许这可以帮助现在仍然有人。当您注册 EditText 时,您必须捕获 OnTouch 事件。

      editText.setOnTouchListener((v, event) -> {
          EditText et = (EditText) v;
          et.onTouchEvent(event);
      
          return false; //very important
       });
      

      不要错过调用showCustomKeyboard(view)方法并隐藏系统键盘的onClickListener。

      【讨论】:

        猜你喜欢
        • 2016-07-18
        • 2017-07-05
        • 2011-02-17
        • 1970-01-01
        • 2019-08-09
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        • 1970-01-01
        相关资源
        最近更新 更多