我正在尝试设置一种方法,一次只能接受一个手势,然后在完成手势之后执行下一个手势。因此,例如,我想从单击开始,然后在您输入水龙头后,它将移动到下一个是双击。这是我正在使用的代码:
import android.content.DialogInterface;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.widget.TextView;
public class secondscreen extends ActionBarActivity implements GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{
private TextView mainMessage; //variable refers to strings.xml// private TextView InText;
private TextView startButton;
private GestureDetectorCompat gestureDetector; // Secondary Variable used for gestures
private DialogInterface.OnClickListener OnClickListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondscreen2);
mainMessage = (TextView) findViewById(R.id.mainMessage);
this.gestureDetector = new GestureDetectorCompat(this, this);
gestureDetector.setOnDoubleTapListener(this);
}
///////// GESTURE METHODS //////////
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
mainMessage.setText("single tap");
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
mainMessage.setText("double tap");
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
mainMessage.setText("double tapping event");
return false;
}
@Override
public boolean onDown(MotionEvent e) {
mainMessage.setText("down");
return false;
}
@Override
public void onShowPress(MotionEvent e) {
mainMessage.setText("showing the press");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
mainMessage.setText("tap up");
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
mainMessage.setText("scrolling is fun");
return false;
}
@Override
public void onLongPress(MotionEvent e) {
mainMessage.setText("very long presses");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
mainMessage.setText("flinging");
return false;
}
///////// GESTURE METHODS //////////
//this next block is needed to run gestures//
@Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_secondscreen, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
我特别希望手势不会在现在设置的同时发生,应用程序将运行手势并显示文本,但它会同时显示最后一个按下 - 我想要知道如何在一个屏幕上设置它以便它要求一个手势并在按下该手势之后移动到下一个手势,最好等待一秒钟,这样就不会立即发生。我很确定它与if else语句有关,但我不知道怎么说它。
答案 0 :(得分:1)
我能想到的一个简单方法就是使用与if语句相结合的枚举
enum ex。
private enum EVENT{
SINGLE_TAP,DOUBLE_TAP,EVENT_3,EVENT_4 ...;
}
private EVENT currentEvent=EVENT.SINGLE_TAP;
事件处理程序
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if (currentEvent==Event.SINGLE_TAP){
mainMessage.setText("single tap");
currentEvent=Event.DOUBLE_TAP;//set to next desired event
return true;
}
return false;
}
在你的其他事件处理程序中执行类似的操作,在最后一个事件中,我会将其循环回第一个
如果您希望事件之间有延迟,请执行秒表并检查事件之间是否已经过了所需的时间。这样的事情。
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
if(stopWatch.getTime > lastEventTime+delay){//pseudo code
if (currentEvent==Event.SINGLE_TAP){
mainMessage.setText("single tap");
currentEvent=Event.DOUBLE_TAP;//set to next desired event
return true;
}
}
return false;
}