Android webview不应该长按显示操作栏

时间:2014-12-20 11:23:44

标签: android android-actionbar android-webview long-press

当我在android webview中选择(长按)文本时,我不想完全显示操作栏,但我需要在屏幕上保持选择。我怎样才能做到这一点。提前致谢。

1 个答案:

答案 0 :(得分:0)

使用触摸列表器实现长按,然后将代码隐藏在按钮Preess [ACTION_DOWN]上的ActionBar中,并在按钮释放时显示动作栏的代码[ACTION_UP或ACTION_MOVE]

  ActionBar actionBar = getActionBar();


   //on long presss hide ur action bar:

textView.setOnTouchListener(new OnTouchListener(){
    private Timer longpressTimer; //won't depend on a motion event to fire
    private final int longpressTimeDownBegin = 500; //0.5 s
    private Point previousPoint;

    switch(event.getAction()){

    case MotionEvent.ACTION_DOWN:{
        longPressTimer = new Timer();
        longpressTimer.schedule(new TimerTask(){
            // hide the action bar
          actionBar.hide();
        }, longpressTimeDownBegin);
        return true; //the parent was also handling long clicks
    }
    case MotionEvent.ACTION_MOVE:{
        Point currentPoint = new Point((int)event.getX(), (int)event.getY());

        if(previousPoint == null){
            previousPoint = currentPoint;
        }
        int dx = Math.abs(currentPoint.x - previousPoint.x);
        int dy = Math.abs(currentPoint.y - previousPoint.y);
        int s = (int) Math.sqrt(dx*dx + dy*dy);
        boolean isActuallyMoving = s >= minDisToMove; //we're moving

        if(isActuallyMoving){ //only restart timer over if we're actually moving (threshold needed because everyone's finger shakes a little)
            cancelLongPress();
            return false; //didn't trigger long press (will be treated as scroll)
        }
        else{ //finger shaking a little, so continue to wait for possible long press
            return true; //still waiting for potential long press
        }
    }
    default:{
        cancelLongPress();

    // show the action bar
    actionBar.show();
        return false;
    }
    }
}

非常简单