Android - 我可以将setOnLongClickListener和setOnClickListener用于同一个按钮吗?

时间:2014-05-14 05:12:51

标签: java android onclicklistener

我真的可以将这些setOnLongClickListener和setOnClickListener用于同一个按钮吗? 因为如果我长按单击按钮,将执行longclick和正常点击,我不知道为什么。我真的可以这样做吗? 请帮帮我:)。

              readDbButton.setOnLongClickListener(
                      new View.OnLongClickListener() {
                          public boolean onLongClick(View view) {
                                //do something
                              return false;
                          }
                      }
              );                  

              readDbButton.setOnClickListener(
              new View.OnClickListener()
              {
                  public void onClick(View view)
                  {
                        //Do something else
                  }
              });

4 个答案:

答案 0 :(得分:8)

onLongClick方法中返回TRUE,以便消耗该事件。

  readDbButton.setOnLongClickListener(
                      new View.OnLongClickListener() {
                          public boolean onLongClick(View view) {
                                //do something
                              return true;
                          }
                      }
              );   

答案 1 :(得分:1)

我得到了你的问题的解决方案。在LongPressed中返回true而不是false。见下文: -

   readDbButton.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {

            Toast.makeText(HomeSafeActivity.this, "Long preess", Toast.LENGTH_LONG).show();

            return true;
        }
    });

也就是说,返回true表示你已经处理了这个事件,它应该在这里停止;如果您没有处理它和/或该事件应该继续任何其他点击监听器

,则返回false

答案 2 :(得分:1)

尝试以正确的方式实施此

public class MainActivity extends Activity {

    private Button button;
    private GestureDetector gestureDetector;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gestureDetector = new GestureDetector(this, new MyGestureDetector());
        button = (Button) findViewById(R.id.button);

        button.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent ev) {
                return gestureDetector.onTouchEvent(ev);
            }
        });
    }

    private class MyGestureDetector extends SimpleOnGestureListener {

        @Override
        public boolean onSingleTapConfirmed(MotionEvent event) {
            Toast.makeText(MainActivity.this, "Single Tap", Toast.LENGTH_SHORT).show();
            return true;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            Toast.makeText(MainActivity.this, "Long Tap", Toast.LENGTH_SHORT).show();
        }

    }

}

答案 3 :(得分:0)

readDbButton.setOnLongClickListener(
                      new View.OnLongClickListener() {
                          public boolean onLongClick(View view) {
                                //do something
Toast.makeText(getBaseContext(), "Long click", Toast.LENGTH_SHORT).show();
                              return true;
                          }
                      }
              );                  

              readDbButton.setOnClickListener(
              new View.OnClickListener()
              {
                  public void onClick(View view)
                  {
                       Toast.makeText(getBaseContext(), "onclick", Toast.LENGTH_SHORT).show();
                  }
              });