今天弄了快一整天,看了很多網上的資料,嘗試了很多種解決方案,但都未能成功。
最終,參考了http://blog.csdn.net/lideguo1979/article/details/7317343的部落格,現把可以啟動並執行代碼,奉獻給大家。。。
沒有使用.xml的資源,為方便和我一樣的新人學習與調試。
package com.example.test;
import android.os.Bundle;
import android.app.Activity;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.TextView;
public class MainActivity extends Activity {
static StringBuilder builder = new StringBuilder();
static TextView textView;
private GestureDetector gd1;
public void onCreate(Bundle state){
super.onCreate(state);
textView = new TextView(this);
textView.setText("Touch and Drag one finger");
gd1 = new GestureDetector(this, new LearnGestureListener());
setContentView(textView);
}
public boolean onTouchEvent(MotionEvent event) {
if (gd1.onTouchEvent(event))
return true;
else
return false;
}
}
class LearnGestureListener extends GestureDetector.SimpleOnGestureListener{
@Override
public boolean onSingleTapUp(MotionEvent ev) {
MainActivity.builder.setLength(0);
MainActivity.builder.append("onSingleTapUp");
MainActivity.textView.setText(MainActivity.builder.toString());
return true;
}
@Override
public void onShowPress(MotionEvent ev) {
MainActivity.builder.setLength(0);
MainActivity.builder.append("onShowPress_LG");
MainActivity.textView.setText(MainActivity.builder.toString());
}
@Override
public void onLongPress(MotionEvent ev) {
MainActivity.builder.setLength(0);
MainActivity.builder.append("onLong_LG");
MainActivity.textView.setText(MainActivity.builder.toString());
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
MainActivity.builder.setLength(0);
MainActivity.builder.append("onScroll_LG");
MainActivity.textView.setText(MainActivity.builder.toString());
return true;
}
@Override
public boolean onDown(MotionEvent ev) {
MainActivity.builder.setLength(0);
MainActivity.builder.append("onDown_LG");
MainActivity.textView.setText(MainActivity.builder.toString());
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
MainActivity.builder.setLength(0);
MainActivity.builder.append("onFling_LG");
MainActivity.textView.setText(MainActivity.builder.toString());
return true;
}
}