【问题标题】:Triggering event continuously when Button is pressed down in Android在Android中按下按钮时连续触发事件
【发布时间】:2012-08-17 18:09:27
【问题描述】:

我有ButtonTextView。最初单击按钮 i 增加计数值并显示在 TextView 中。我以前这样做的代码是。

buttonDayPlus.setOnClickListener(new OnClickListener() 
{               
     @Override
     public void onClick(View v) 
     {
     count = count + 1;
         textView.setText(Integer.toString(count));
     }
}

但是现在我想要的是如果用户按下按钮,那么我想一个接一个地增加值并在textView 中显示增量值,并且一旦用户抬起手......最后增量值显示在textView 中。所以如果有人知道帮助我解决这个问题

【问题讨论】:

标签: android


【解决方案1】:

您将需要使用 Handler 对象,因为您必须实现一个单独的线程来进行递增/递减

public class MainActivity extends Activity {

    private boolean autoIncrement = false;
    private boolean autoDecrement = false;
    private final long REPEAT_DELAY = 50;
    private Handler repeatUpdateHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.input);

        class RepetitiveUpdater implements Runnable {

            @Override
            public void run() {
                if (autoIncrement) {
                    increment();
                    repeatUpdateHandler.postDelayed(new RepetitiveUpdater(), REPEAT_DELAY);
                } else if (autoDecrement) {
                    decrement();
                    repeatUpdateHandler.postDelayed(new RepetitiveUpdater(), REPEAT_DELAY);
                }
            }

        }

        up.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                increment();
            }
        });

        up.setOnLongClickListener(new OnLongClickListener() {

            @Override
            public boolean onLongClick(View v) {
                autoIncrement = true;
                repeatUpdateHandler.post(new RepetitiveUpdater());
                return false;
            }
        });

        up.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP && autoIncrement) {
                    autoIncrement = false;
                }
                return false;
            }
        });

    }

    public void increment() {
        if (i < 100) {
            i++;
            TextView no = (TextView) findViewById(R.id.no);
            no.setText(String.valueOf(i));
        }

    }

}

减量也一样。

礼貌:Github,作者:Jeffrey F. Cole

【讨论】:

    【解决方案2】:

    我刚刚在这里回答了这个问题 https://stackoverflow.com/a/41466381/2991628

    我创建了一个CounterHandler 类来完成所有计数和事件处理。这就是它的工作原理。

     new CounterHandler.Builder()
                .incrementalView(buttonPlus)
                .decrementalView(buttonMinus)
                .minRange(-50) // cant go any less than -50
                .maxRange(50) // cant go any further than 50
                .isCycle(true) // 49,50,-50,-49 and so on
                .counterDelay(200) // speed of counter
                .counterStep(2)  // steps e.g. 0,2,4,6...
                .listener(this) // to listen counter results and show them in app
                .build();
    

    你可以从我的 gist 中找到这门课 https://gist.github.com/nomanr/d142f4ccaf55ceba22e7f7122b55b9b6

    【讨论】:

      【解决方案3】:

      您应该使用OnTouchListener 而不是 OnClickListener。

      但是,您的问题含糊不清。如果您只想在释放按钮时增加一次值,您可以执行以下操作:

      button.setOnTouchListener(new OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              if (event.getAction() == MotionEvent.ACTION_UP) {
                  count++;
              }
          }
      };
      

      如果您想在用户按住按钮时增加计数,您可以这样做:

      button.setOnTouchListener(new OnTouchListener() {
          @Override
          public boolean onTouch(View v, MotionEvent event) {
              count++;
              myTextView.setText("Click count: "+count);
          }
      };
      

      myTextView 当然是你想要显示点击的文本视图。

      【讨论】:

      • 但在我的情况下,只要用户按下按钮,我就必须在 textview 中向用户显示计数值的变化。我的意思是说如果按住按钮,我将如何让计数值递增/递减并在 textview 中显示?
      • 为什么你的布尔值没有返回任何值@EmirKuljanin?
      【解决方案4】:

      像这样使用自定义触摸监听器,

      public class CustomTouchListener implements View.OnTouchListener {     
          public boolean onTouch(View view, MotionEvent motionEvent) {
          switch(motionEvent.getAction()){            
                  case MotionEvent.ACTION_DOWN:
                 count = count + 1;
                      break;          
                  case MotionEvent.ACTION_CANCEL:             
                  case MotionEvent.ACTION_UP:
                textView.setText(Integer.toString(count));
                      break;
          } 
              return false;   
          } 
      }
      

      并将这些侦听器设置为您的按钮。希望对你有帮助。

      【讨论】:

        【解决方案5】:

        @shalini 给出的答案是正确的,但根据您的要求几乎没有变化

        public class CustomTouchListener implements View.OnTouchListener {     
            public boolean onTouch(View view, MotionEvent motionEvent) {
            switch(motionEvent.getAction()){            
                    case MotionEvent.ACTION_DOWN:
                       count = count + 1;
                       textView.setText(Integer.toString(count));
                    break;          
        
                   case MotionEvent.ACTION_CANCEL:             
        
                   case MotionEvent.ACTION_UP:
        
                        break;
            } 
                return false;   
            } 
        } 
        

        【讨论】:

          猜你喜欢
          • 2011-02-06
          • 2010-12-31
          • 1970-01-01
          • 2023-03-03
          • 2013-10-11
          • 2021-07-18
          • 2018-06-11
          • 1970-01-01
          • 2019-06-07
          相关资源
          最近更新 更多