如何在横向软键盘中获取光标位置

时间:2017-11-14 09:15:20

标签: android android-softkeyboard ontouchlistener

如何在横向软键盘中获取光标位置。在纵向模式下,我可以使用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;
      }
});

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

1 个答案:

答案 0 :(得分:0)

您是对的,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
    }
}
相关问题