繁体   English   中英

onTouchEvent 和 onClickListener 不能在同一个页面上一起工作

[英]onTouchEvent and onClickListener can't work together on the same page

For example, if I have some buttons with onclick listener and the rest of the screen recieves ontouch event.The ontouch event will jam the onclick event unless you lift up the ontouch finger the onclick will not respond.

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);        
    mAttackButton        = findViewById(R.id.attack);
    mJumpButton          = findViewById(R.id.jump);
    mAttackButton.setOnClickListener(this);
    mJumpButton  .setOnClickListener(this);}

   @Override
   public void onClick(View view) {
    switch (view.getId()) {
        case R.id.attack:
        attack();
        break;
        case R.id.jump;
        jump();
        break;
     }
   }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
     super.onTouchEvent(event);
     mTouchPoint.x=event.getX();
     mTouchPoint.y=event.getY();
     return true;
 }
}

您始终可以拆分 onClickListener()。 而不是实现整个活动。 尝试将 onClickListener() 分别添加到每个视图。 与 OnTouchEvent() 相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM