【问题标题】:How to detect double click event for buttons in android如何检测android中按钮的双击事件
【发布时间】:2015-04-25 04:14:14
【问题描述】:

我有什么:我在水平滚动视图中有一组按钮

我想要什么:我想检测双击按钮的事件

注意 通常我使用class implements Onclicklistener 处理点击事件。但是现在我如何才能为双击事件实现相同的目标。如果有任何接口我也可以用于此目的,那将会很有帮助


代码:

<HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/button1"
                android:layout_width="200dp"
                android:layout_height="150dp"
                android:text="Song1" />

            <Button
                android:id="@+id/button2"
                android:layout_width="200dp"
                android:layout_height="150dp"
                android:text="Song2" />

            <Button
                android:id="@+id/button3"
                android:layout_width="200dp"
                android:layout_height="150dp"
                android:text="Song3" />
        </LinearLayout>
    </HorizontalScrollView>

【问题讨论】:

标签: android


【解决方案1】:
private int buttonClick = 0;
private void fabVisibility () {
    fabOpen.setOnClickListener (v -> {
        if (buttonClick == 0){
            buttonClick = 2;
            overridePendingTransition (R.anim.fade_in, R.anim.fade_out);
            scanFab.setVisibility (View.VISIBLE);
            historyFab.setVisibility (View.VISIBLE);
        }else if (buttonClick == 2){
            buttonClick = 0;
            overridePendingTransition (R.anim.fade_in, R.anim.fade_out);
            scanFab.setVisibility (View.GONE);
            historyFab.setVisibility (View.GONE);
        }
    });
}

【讨论】:

    【解决方案2】:

    我用这个独奏:

        var doubleTap = false
        your_button.setOnClickListener {
            if (doubleTap)
                handleClick(it.context)
            doubleTap = true
            Handler(Looper.getMainLooper()).postDelayed({
                doubleTap = false
            }, 1500)
        }
    

    你可以在 onBackPress Activity 中使用这个技巧来退出应用程序。

    祝你好运

    【讨论】:

      【解决方案3】:

      这对我有用,

      注意:它只适用于双击,它不能单独处理单击和双击动作

      // Define 2 global class variables
      private static long LAST_CLICK_TIME = 0;
      private final int mDoubleClickInterval = 400; // Milliseconds
      
      // Then, inside onClick method of Button
      long doubleClickCurrentTime = System.currentTimeMillis();
      long currentClickTime = System.currentTimeMillis();
      if (currentClickTime - LAST_CLICK_TIME <= mDoubleClickInterval)
      {
          Toast.makeText(this, "Double click detected", Toast.LENGTH_SHORT).show();
      }
      else
      {
          LAST_CLICK_TIME = System.currentTimeMillis();
          // !Warning, Single click action problem
      }
      

      【讨论】:

        【解决方案4】:

        这里有一个很好的答案:

        Single click and double click of a button in android

        不过,我建议使用 Google 推荐的 OnLongPress 事件。

        在此处查看该 API:

        http://developer.android.com/reference/android/view/View.OnLongClickListener.html

        干杯

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-05
          • 1970-01-01
          • 2011-01-14
          • 2015-11-09
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多