如何避免在Android Kitkat 4.4中长按默认选择?

时间:2013-12-05 04:57:33

标签: java android webview

Hello Developers,                  我在这里与Btwebview长期合作我们正在避免长按的默认选择功能并给予我们自己。长按方法的覆盖工作完美,直到android 4.3但是4.4的defalut选择也随行动条。我是我提到示例代码 -

public class BTWebView extends WebView implements TextSelectionJavascriptInterfaceListener, OnTouchListener, OnLongClickListener,DragListener    {
.......
public BTWebView(Context context) {
        super(context);
        this.ctx = context;
        this.setup(context);
    }
    protected void setup(Context context)
        {
        this.setOnLongClickListener(this);
        this.setOnTouchListener(this);

         }

并长按

   @Override
    public boolean onLongClick(View v)
    {
       ......
    return true;  
    }
}

这里覆盖了长按和返回值为真,所以它避免默认选择到4.3所以请告诉我如何避免完整的默认选择或至少避免操作栏长按。提前谢谢

3 个答案:

答案 0 :(得分:3)

我在github上找到了这个问题的解决方案,它是 -

 if(event.getAction() == MotionEvent.ACTION_UP)
        {       
              if(!mScrolling){
                  mScrolling = false;
                  endSelectionMode();
                  return false;
              }

              mScrollDiffX = 0;
              mScrollDiffY = 0;
              mScrolling = false;

              // Fixes 4.4 double selection
              return true;
        }

这里你也必须返回true,之后默认选择不会出现。

答案 1 :(得分:1)

Ravi Saini提出的KitKat及以上解决方案可以防止出现默认选择界面。但是,对于ACTION_UP事件返回true会阻止用于快速垂直滚动内容的fling手势,因此使用WebView似乎不自然。我添加了 isInSelectionMode()条件来防止这种情况,现在fling手势工作正常,除非在选择模式下。来自WebViewMarker GitHub project的代码与我的更改如下(在TextSelectionSupport.java模块中,onTouch()方法):

    case MotionEvent.ACTION_UP:
        if (!mScrolling) {
            endSelectionMode();
            //
            // Fixes 4.4 double selection
            // See: http://stackoverflow.com/questions/20391783/how-to-avoid-default-selection-on-long-press-in-android-kitkat-4-4
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                return false;
            }
        }
        mScrollDiffX = 0;
        mScrollDiffY = 0;
        mScrolling = false;
        //
        // Fixes 4.4 double selection
        // See: http://stackoverflow.com/questions/20391783/how-to-avoid-default-selection-on-long-press-in-android-kitkat-4-4
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && isInSelectionMode()) {
            return true;
        }
        break;

格雷格

答案 2 :(得分:0)

如果未调用onLongClick()方法并且WebView中的长按正在启用用户选择(即复制,剪切,粘贴等),那么您可以尝试用户选择CSS属性

user-select:none;

哪个会压制这种行为,虽然不确定这是否仍会触发长按一下监听器,或者WebView是否会继续继承点击事件。