OnTouchEvent 中仔细选择文本
我写了一个代码来选择文本的一部分,但是我找不到开始和结束的确切点。
我的代码:
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
{
lastX = (int) event.getX();
lastY = (int) event.getY();
break;
}
case MotionEvent.ACTION_UP:
{
int currentX = (int) event.getX();
int currentY = (int) event.getY();
int xDistance = Math.abs(currentX - lastX);
int yDistance = Math.abs(currentY - lastY);
final Spannable WordtoSpan = (Spannable) mText;
int line = mLayout.getLineForVertical(yDistance);
int offset = mLayout.getOffsetForHorizontal(line, xDistance);
WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), line,
offset, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
txtContent.setText(WordtoSpan);
break;
}
}
}
请帮助我,我真的需要它。
I wrote a code for select a part of text but But I can't find the exact point of beginning and ending.
My code:
@Override
public boolean onTouchEvent(MotionEvent event) {
final int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
{
lastX = (int) event.getX();
lastY = (int) event.getY();
break;
}
case MotionEvent.ACTION_UP:
{
int currentX = (int) event.getX();
int currentY = (int) event.getY();
int xDistance = Math.abs(currentX - lastX);
int yDistance = Math.abs(currentY - lastY);
final Spannable WordtoSpan = (Spannable) mText;
int line = mLayout.getLineForVertical(yDistance);
int offset = mLayout.getOffsetForHorizontal(line, xDistance);
WordtoSpan.setSpan(new BackgroundColorSpan(0xFFFFFF00), line,
offset, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
txtContent.setText(WordtoSpan);
break;
}
}
}
Please help me, I really need it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论