简体   繁体   English

onTouchListener 不工作

[英]onTouchListener not working

I have the following code in my activity.我的活动中有以下代码。 In my xml, the video view is inside the linear layout.在我的 xml 中,视频视图位于线性布局内。 However, when the view is clicked, the onTouchListener never fires.但是,当单击视图时, onTouchListener永远不会触发。 I tried changing the onTouchListener to vvLive but that didn't do anything.我尝试将onTouchListener更改为vvLive ,但这并没有做任何事情。 I also tried changing the onTouchListener to an onClickListener , but nothing.我还尝试将onTouchListener更改为onClickListener ,但没有。 Anyone know why the listener isn't firing?有谁知道为什么听众没有开火? Thanks.谢谢。

        private VideoView vvLive;
        LinearLayout linearLayoutLiveVideo;

        linearLayoutLiveVideo.setOnTouchListener(new OnTouchListener(){
            public boolean onTouch(View v, MotionEvent event){
                Log.d(TAG, "onTouch entered");
                if(event.getAction() == MotionEvent.ACTION_UP) {
                    Log.d(TAG, "ACTION_UP");

                }
                return false;
            }
        });

EDIT : I realized the code above actually works.编辑:我意识到上面的代码确实有效。 Something in eclipse was messing up LogCat. eclipse 中的某些东西弄乱了 LogCat。 After I restarted eclipse, LogCat prints the first log "onTouch entered".在我重新启动 eclipse 后,LogCat 打印第一个日志“onTouch 输入”。 However, "ACTION_UP" was not being printed.但是,没有打印“ACTION_UP”。 I changed the MotionEvent to MotionEvent.ACTION_DOWN and the LogCat prints now.我将 MotionEvent 更改为MotionEvent.ACTION_DOWN并且 LogCat 现在打印。 Why does ACTION_DOWN work but ACTION_UP does not?为什么ACTION_DOWN有效而ACTION_UP无效?

ACTION_UP is never being sent to your listener because you return false and therefor don't "consume" the event. ACTION_UP 永远不会发送给您的听众,因为您返回 false ,因此不要“消费”该事件。 Return true and you'll get the start event (ACTION_DOWN) as well as all the subsequent ones (ACTION_MOVE and then ACTION_UP).返回true,您将获得开始事件(ACTION_DOWN)以及所有后续事件(ACTION_MOVE,然后是ACTION_UP)。

Modify your code like this and try:像这样修改您的代码并尝试:

@Override
public boolean onTouchEvent(MotionEvent event) {
    Log.d(TAG, "onTouch entered");
    if(event.getAction() == MotionEvent.ACTION_UP) {
        Log.d(TAG, "ACTION_UP");
        return super.onTouchEvent(event);
    else
        return false;
}

i have faces this issue and the solutions is:-我面临这个问题,解决方案是: -

1-in your xml set the followin attribute to VideoView 1-在您的 xml 中将以下属性设置为 VideoView

android:clickable="true" android:clickable="true"

2- simply in your code set setOnClickListenerto the VideoView and it will work like charm: 2-只需在您的代码集中 setOnClickListener 到 VideoView,它就会像魅力一样工作:

videoView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent=new Intent(CinemaDetailsActivity.this , FullScreenPlayerActivity.class);
        intent.putExtra("url" ,  getIntent().getStringExtra("url"));
        startActivity(intent);
    }
});

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

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