【问题标题】:how to get cursor position in soft keyboard in landscape如何在横向软键盘中获取光标位置
【发布时间】:2017-11-14 09:15:20
【问题描述】:

如何在横向软键盘中获取光标位置。在纵向模式下我可以使用 OnTouchListener,但在横向模式下,OnTouchListener 在全屏软键盘上不起作用。

editText.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View view, MotionEvent motionEvent) {
            cursorPosition = editText.getOffsetForPosition(motionEvent.getX(), motionEvent.getY()); // will give you the position of the nearest chracter.
            return false;
      }
});

简而言之,我该如何在横向全屏键盘上执行上述操作..

【问题讨论】:

    标签: android android-softkeyboard ontouchlistener


    【解决方案1】:

    你是对的,OnTouchListener 在 EditText 的全屏模式下将不起作用。但是,如果它对您有帮助,您可以使用在光标位置更改时调用的侦听器。为此,请扩展 EditText 并覆盖其 onSelectionChanged 方法:

    public class CustomEditText extends EditText
    {
        public CustomEditText(Context context)
        {
            super(context);
        }
    
        public CustomEditText(Context context, AttributeSet attrs)
        {
            super(context, attrs);
        }
    
        @Override
        protected void onSelectionChanged(int selStart, int selEnd)
        {
            super.onSelectionChanged(selStart, selEnd);
           // here you get cursor position change
        }
    }
    

    【讨论】:

    • onSelectionChanged 用于将 SelectionSpan 应用于字符序列但我想要用户触摸的位置。这就是我首先使用OnTouchListener 的原因。
    猜你喜欢
    • 2012-03-29
    • 2013-10-26
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多