Ứng dụng của tôi chứa một khu vực chứa đầy các nút. Tôi muốn thực hiện các hoạt động theo cách như vậy, rằng cử chỉ fling trên khu vực nút sẽ chuyển nó sang một trong hai khu vực khác (bằng cách sử dụng ViewFlipper).Android, cử chỉ trên các tiện ích có thể nhấp
Tôi đã thực hiện hai phương pháp phát hiện cử chỉ. Người đầu tiên tham gia sử dụng GestureDetector. Tuy nhiên, các sự kiện chuyển động cảm ứng trên Button đã không làm tăng phương thức hoạt động onTouchEvent, vì vậy - kết quả là tôi không thể chuyển tiếp nó tới lớp GestureDetector. Một thất bại, trong ngắn hạn.
Cách tiếp cận thứ hai - liên quan đến sử dụng GestureOverlayView. Lần này, tuy nhiên, tôi đã đạt đến cực điểm thứ hai: không chỉ cử chỉ được phát hiện, mà còn là nút, qua đó cử chỉ được thực hiện, báo cáo một cú nhấp chuột.
Tôi muốn giao diện hoạt động theo cách sau: nếu người dùng chạm vào nút và nhả cảm ứng (hoặc di chuyển ngón tay chỉ một chút), nút báo cáo nhấp và không có cử chỉ nào được phát hiện. Mặt khác, nếu người dùng chạm vào màn hình và di chuyển lâu hơn, cử chỉ sẽ được phát hiện và không có sự kiện nhấp được báo cáo bởi nút.
Tôi đã triển khai một ứng dụng chứng minh khái niệm nhỏ. Hệ thống mã ngành XML sau:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical">
<android.gesture.GestureOverlayView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/overlay">
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
<TextView android:id="@+id/display" android:layout_width="match_parent" android:layout_height="wrap_content" />
<Button android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/button"/>
</LinearLayout>
</android.gesture.GestureOverlayView>
</LinearLayout>
Hoạt động java code sau:
package spk.sketchbook;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.gesture.*;
import android.gesture.GestureOverlayView.OnGestureListener;
public class Main extends Activity implements OnGestureListener, OnClickListener
{
private void SetupEvents()
{
GestureOverlayView ov = (GestureOverlayView)findViewById(R.id.overlay);
ov.addOnGestureListener(this);
Button b = (Button)findViewById(R.id.button);
b.setOnClickListener(this);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SetupEvents();
}
@Override
public void onGesture(GestureOverlayView arg0, MotionEvent arg1)
{
TextView tv = (TextView)findViewById(R.id.display);
tv.setText("Gesture");
}
@Override
public void onGestureCancelled(GestureOverlayView arg0, MotionEvent arg1)
{
}
@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event)
{
}
@Override
public void onGestureStarted(GestureOverlayView overlay, MotionEvent event)
{
}
@Override
public void onClick(View v)
{
TextView tv = (TextView)findViewById(R.id.display);
tv.setText("Click");
}
}
Câu hỏi đặt ra là: làm thế nào để thực hiện một giao diện như vậy, mà có thể quyết định, nếu hành động người dùng sẽ được coi là cử chỉ hoặc bấm nút?
Trân trọng - Spook.
http://stackoverflow.com/questions/937313/android-basic-gesture-detection Có thể giúp đỡ nếu ai đó không trả lời trực tiếp – CityView