android:如何从触摸事件中获取文本位置

android: How to get text position from touch event(android:如何从触摸事件中获取文本位置)
本文介绍了android:如何从触摸事件中获取文本位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我想实现一个自定义文本界面,触摸+拖动选择文本并且键盘不被抬起,这与长按打开 CCP 菜单和键盘的默认行为形成对比.我的理解表明我需要这种方法:

I'm wanting to implement a custom text interface, with touch+drag selecting text and the keyboard not being raised, in contrast to the default behavior of a long-click bringing up the CCP menu and the keyboard. My understanding suggests I need this approach:

onTouchEvent(event){
  case touch_down:
    get START text position

  case drag
    get END text position
    set selection range from START to END
}

我已经了解了所有关于 getSelectStart() 和设置范围等的各种方法,但我找不到如何根据触摸事件 getX() 和 getY() 获取文本位置.有没有办法做到这一点?我已经在其他办公应用中看到了我想要的行为.

I've found out all about getSelectStart() and various methods to setting a range and such, but I cannot find how to get the text position based on a touch event getX() and getY(). Is there any way to do this? I've seen the behaviour I want in other office apps.

另外,在手动请求之前,我将如何阻止键盘出现?

Also, how would I stop the keyboard appearing until manually requested?

推荐答案

"mText.setInputType(InputType.TYPE_NULL)" 在 Android 3.0 及以上版本下会抑制软键盘,但也会禁用 EditText 框中闪烁的光标.我编写了一个 onTouchListener 并返回 true 以禁用键盘,然后必须从运动事件中获取触摸位置以将光标设置到正确的位置.您可以在 ACTION_MOVE 运动事件上使用它来选择要拖动的文本.

"mText.setInputType(InputType.TYPE_NULL)" will suppress the soft keyboard but it also disables the blinking cursor in an EditText box under Android 3.0 and above. I coded an onTouchListener and returned true to disable the keyboard and then had to get the touch position from the motion event to set the cursor to the correct spot. You might be able to use this on an ACTION_MOVE motion event to select text for dragging.

这是我使用的代码:

  mText = (EditText) findViewById(R.id.editText1);
  OnTouchListener otl = new OnTouchListener() {
  @Override
  public boolean onTouch(View v, MotionEvent event) {
      switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
          Layout layout = ((EditText) v).getLayout();
          float x = event.getX() + mText.getScrollX();
          int offset = layout.getOffsetForHorizontal(0, x);
          if(offset>0)
              if(x>layout.getLineMax(0))
                  mText.setSelection(offset);     // touch was at end of text
              else
                  mText.setSelection(offset - 1);
          break;
          }
      return true;
      }
  };
  mText.setOnTouchListener(otl);

这篇关于android:如何从触摸事件中获取文本位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Prevent enter key on EditText but still show the text as multi-line(防止在 EditText 上输入键,但仍将文本显示为多行)
Android keyboard next button issue on EditText(EditText上的Android键盘下一个按钮问题)
When the soft keyboard appears, it makes my EditText field lose focus(当软键盘出现时,它使我的 EditText 字段失去焦点)
android how to make text in an edittext exactly fixed lines(android如何在edittext中制作文本完全固定的行)
How to use regular expression in Android(如何在 Android 中使用正则表达式)
Filter list view from edit text(从编辑文本中过滤列表视图)