简体   繁体   English

OnTouchListener视图始终相同

[英]OnTouchListener view is always the same

I have an Activity which implements OnTouchListener . 我有一个实现OnTouchListener的Activity。 I then have a couple of custom view that are registered to this. 然后,我有一些注册到此的自定义视图。

customView1.setOnTouchListener(this);
customView2.setOnTouchListener(this);

I then have this: 然后我有这个:

    @Override
        public boolean onTouch(View view, MotionEvent event) {

            if (view == customView1); {

                t = Toast.makeText(this, "customView1", Toast.LENGTH_SHORT);
                t.show();
            }
}

The problem is that the Toast is shown even when the view clicked is not customView1. 问题是即使单击的视图不是customView1,也会显示Toast。

use View.getId() to get onTouch on different view instead of comparing instance's : 使用View.getId()在不同的视图上获取onTouch而不是比较实例:

   if (view.getId() == customView1.getId()) {

        t = Toast.makeText(this, "customView1", Toast.LENGTH_SHORT);
        t.show();
    }

and also remove semicolon(;) after if condition to execute if block 并且如果条件执行条件,则删除semicolon(;)

You have a semicolon right after if statement. if语句后面有一个分号。 So if condition is useless in your case. 所以如果你的情况没有条件。

So remove ; 所以删除; after below statement 在以下声明之后

 if (view == customView1);

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

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