OnClick和Swiping组合

时间:2013-12-04 05:35:59

标签: android

我正在尝试制作类似于Android相机ui的ui。在基本形式中,它看起来像这样

enter image description here

我希望能够在按钮上注册点击事件。如果用户按下并向右滑动,则会选择显示正确文本视图的多士,反之亦然。我现在的代码看起来像这样。

public class MainActivity extends Activity implements View.OnTouchListener{

    private RelativeLayout overLay;
    private TextView leftTv, rightTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        overLay = (RelativeLayout) findViewById(R.id.overLay);
        leftTv = (TextView) findViewById(R.id.leftTv);
        rightTv = (TextView) findViewById(R.id.rightTv);
        leftTv.setOnTouchListener(this);
        rightTv.setOnTouchListener(this);
    }

    public void buttonOnClick(View view) {
        Toast.makeText(this, "single", Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.d(MainActivity.class.getSimpleName(), "action id: " + event.getAction());
        if (v.getId() == R.id.leftTv) {
            Toast.makeText(this, "left", Toast.LENGTH_SHORT).show();
        } else if (v.getId() == R.id.rightTv) {
            Toast.makeText(this, "right", Toast.LENGTH_SHORT).show();
        }
        return false;
    }
}

2个文本视图延伸到按钮的中心。

目前,如果我点击按钮显示的文本视图的所有吐司首先按钮,则点击按钮的吐司显示。如果我单击按钮右侧,则向右滑动向右滑动显示。

我如何获得所需的结果。有没有办法检测用户手指在移除时哪个视图结束了?

3 个答案:

答案 0 :(得分:0)

这需要手势监听器来监听用户的触摸并且用户释放触摸。 这是手势检测和动作的小example

答案 1 :(得分:0)

答案 2 :(得分:0)

你可以在android中使用Gesture监听器。

让我解释一下

1.创建一个抽象类OnFlingGestureListeners和implementsmnts OnTouchListener

像这样的代码sholud

public abstract class OnFlingGestureListeners implements OnTouchListener {

private final GestureDetector gdt = new GestureDetector(new GestureListener());

  @Override
  public boolean onTouch(final View v, final MotionEvent event) {
      gdt.onTouchEvent(event);
      return true;
  }

  private final class GestureListener extends SimpleOnGestureListener {

     private static final int SWIPE_MIN_DISTANCE = 10;
     private static final int SWIPE_THRESHOLD_VELOCITY = 10;
     private static final int SWIPE_MAX_OFF_PATH = 250;


     @Override
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
         if(Math.abs(e1.getY()-e2.getY())>SWIPE_MAX_OFF_PATH)
         {
             return false;
         }
        if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
           onRightToLeft();
           return true;
        } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
           onLeftToRight();
           return true;
        }
        if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
           onBottomToTop();
           return true;
        } else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
           onTopToBottom();
           return true;
        }

        return false;
     }

     @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
         onTouchDown();
        return true;
    }




  }

  public abstract void onRightToLeft();

  public abstract void onLeftToRight();

  public abstract void onBottomToTop();

  public abstract void onTopToBottom();

  public abstract void onTouchDown();

}

2.设置按钮上的触控器。就像

    button.setOnTouchListener(new OnFlingGestureListeners() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            return super.onTouch(v, event);
        }

        @Override
        public void onRightToLeft() {
            // TODO Auto-generated method stub




        }

        @Override
        public void onLeftToRight() {
            // TODO Auto-generated method stub


        }

        @Override
        public void onBottomToTop() {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTopToBottom() {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTouchDown() {
            // TODO Auto-generated method stub


        }

    });
}

你也可以单独使用onclick监听器。

希望这会有所帮助