基于长preSS位置动态修改上下文/庆龙preSS菜单中的EditText [英] Dynamically Modifying Contextual/Long-Press Menu in EditText Based on Position of Long Press

查看:138
本文介绍了基于长preSS位置动态修改上下文/庆龙preSS菜单中的EditText的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用一个EditText小部件,并想修改,当用户长时间presses的观点所显示的上下文菜单。那我遇到的问题是,我需要知道的长preSS文本中的字符位置,所以我能确定我需要添加到右键菜单。基类是这样做的,因为在菜单中的选项之一是添加word_clicked_on到字典。设置文本内ClickableSpans不会出现是一个解决方案,因为它消耗的点击事件,这使得它不可能将编辑光标移动的跨度范围内。

I am using an EditText widget and would like to modify the context menu that is displayed when the user long presses the view. The problem that I am having is that I need to know the character position within the text of the long press so I can determine what I need to add to the context menu. The base class is doing this because one of the choices in the menu is 'Add "word_clicked_on" To Dictionary.' Setting ClickableSpans within the text does not appear to be a solution since it consumes the click event which makes it impossible to move the edit cursor within the spans.

推荐答案

下面是我想出了一个解决方案,它的工作,所以我想和大家分享吧:

Here is the solution that I came up with and it does work so I wanted to share it:

首先,我的结论是,我需要延长的EditText类,这样我可以拦截的onTouchEvent,捕捉ACTION_DOWN事件,并保存位置。现在,我有向下点的位置,我可以打电话getOffsetForPosition(downPointX,downPointY)和获得的长期preSS的字符位置。有一个很大的问题,getOffsetForPosition未添加至SDK 14!为了使这个解决方案的工作,我不得不回港getOffsetForPosition的功能和分支,如果当前的SDK早于SDK_INT 14.这是源$ C ​​$ C为新类:

First I concluded that I needed to extend the EditText class so that I could intercept the onTouchEvent, capture the ACTION_DOWN event, and save the position. Now that I have the position of the down point I can call getOffsetForPosition(downPointX, downPointY) and get the character position of the long-press. There is one big problem, getOffsetForPosition was not added until SDK 14! To make this solution work I had to back port the functionality of getOffsetForPosition and branch if the current SDK is earlier than SDK_INT 14. Here is the source code for the new class:

public class ScrapEditText extends EditText{

protected float LastDownPositionX, LastDownPositionY;

public ScrapEditText(Context context) 
{
    super(context);
}

public ScrapEditText(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
    final int action = event.getActionMasked();
    if(action == MotionEvent.ACTION_DOWN)
    {
        LastDownPositionX = event.getX();
        LastDownPositionY = event.getY();
    }
    return super.onTouchEvent(event);
}

public float GetLastDownPositionX()
{
    return LastDownPositionX;
}

public float GetLastDownPositionY()
{
    return LastDownPositionY;
}

public int GetOffsetForLastDownPosition()
{

    if(Build.VERSION.SDK_INT > 13)
    {
        // as of SDK 14 the getOffsetForPosition was added to TextView
        return getOffsetForPosition(LastDownPositionX, LastDownPositionY);
    }
    else
    {
        return GetOffsetForPositionOlderSdk();
    }
}

public int GetOffsetForPositionOlderSdk()
{
    if (getLayout() == null) return -1;
    final int line = GetLineAtCoordinateOlderSDK(LastDownPositionY);
    final int offset = GetOffsetAtCoordinateOlderSDK(line, LastDownPositionX);
    return offset;
}

public int GetLineAtCoordinateOlderSDK(float y)
{
    y -= getTotalPaddingTop();
    // Clamp the position to inside of the view.
    y = Math.max(0.0f, y);
    y = Math.min(getHeight() - getTotalPaddingBottom() - 1, y);
    y += getScrollY();
    return getLayout().getLineForVertical((int) y);     
}

protected int GetOffsetAtCoordinateOlderSDK(int line, float x) {
    x = ConvertToLocalHorizontalCoordinateOlderSDK(x);
    return getLayout().getOffsetForHorizontal(line, x);
}

protected float ConvertToLocalHorizontalCoordinateOlderSDK(float x) {
    x -= getTotalPaddingLeft();
    // Clamp the position to inside of the view.
    x = Math.max(0.0f, x);
    x = Math.min(getWidth() - getTotalPaddingRight() - 1, x);
    x += getScrollX();
    return x;
}

}

在你的活动派生类中:

ScrapText = (ScrapEditText) findViewById(R.id.sample_text);  
ScrapText.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener(){

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) 
{
    int charOffset = FileText.GetOffsetForLastDownPosition();
}           
 });

这篇关于基于长preSS位置动态修改上下文/庆龙preSS菜单中的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆