Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android,可点击小部件上的手势_Android_User Interface_Interface_Gestures_Clickable - Fatal编程技术网

Android,可点击小部件上的手势

Android,可点击小部件上的手势,android,user-interface,interface,gestures,clickable,Android,User Interface,Interface,Gestures,Clickable,我的应用程序包含一个充满按钮的区域。我希望以这样的方式实现该活动,即在按钮区域上投掷手势会将其切换到另两个区域之一(使用ViewFlipper) 我提出了两种检测手势的方法。第一个涉及使用手势检测器。然而,按钮上的触摸动作事件并没有引发onTouchEvent活动方法,因此-结果-我无法将其转发到GestureDetector类。简言之,这是一次失败 第二种方法-使用GestureOverlayView。然而,这一次,我达到了第二个极端:不仅检测到了手势,而且执行手势的按钮也报告了一次点击 我希

我的应用程序包含一个充满按钮的区域。我希望以这样的方式实现该活动,即在按钮区域上投掷手势会将其切换到另两个区域之一(使用ViewFlipper)

我提出了两种检测手势的方法。第一个涉及使用手势检测器。然而,按钮上的触摸动作事件并没有引发onTouchEvent活动方法,因此-结果-我无法将其转发到GestureDetector类。简言之,这是一次失败

第二种方法-使用GestureOverlayView。然而,这一次,我达到了第二个极端:不仅检测到了手势,而且执行手势的按钮也报告了一次点击

我希望界面以以下方式工作:如果用户触摸按钮并释放触摸(或只移动手指一点点),按钮报告单击,并且没有检测到任何手势。另一方面,如果用户触摸屏幕并进行较长的移动,则应检测到该手势,且按钮不会报告任何点击事件

我已经实现了一个小型的概念验证应用程序。活动XML代码如下所示:

<?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>
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");
  }


}
问题是:如何实现这样一个界面,它可以决定用户的动作应该被视为手势还是按钮点击


致以最诚挚的问候——斯派克。

我自己解决了。这个想法是从GestureOverlayView捕捉手势事件,除了将它们传递给GestureDetector之外,还可以测量用户手势的距离。距离应存储在私人活动的字段中(因此可用于所有事件处理程序)。最后,fling和click处理程序应检查此字段的值;如果低于某个值(比如说,10个像素就可以了),则应解释为单击。否则——作为一种姿态

请注意,按钮将显示为已单击,并将调用其单击处理程序。我决定创建一个低级单击处理程序(附加到GestureOverlayView上的所有按钮),它执行手势/单击检查,如果结果是单击,则选择一个高级单击处理程序

这个解决方案对我有效;但是,如果希望禁用按钮的单击外观并阻止调用处理程序,则可能需要重新定义按钮和/或GestureOverlayView组件。

另一种解决方案: 使此功能:

public void SetSwipe(View v, final GestureDetector gestureScanner){
    // Sets scroll view listener
    v.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
              if (gestureScanner.onTouchEvent(event))  
                return true;  
              else  
                return false;  
        }
    });
}
并为每个小部件调用它,如下所示:

scfSetTag.SetSwipe(svAllView, gestureScanner);
如果需要从许多活动或片段中创建该类,可以创建一个class.java文件,并在每个需要实现滑动的活动上实例化该类。 这会检测到在任何Widget上的滑动和单击。 希望有帮助

如果有人不直接回答,可能会有帮助