Android: Why can't I give an onClickListener t

2019-03-14 05:46发布

I have written these lines of code:

 mVideoView = (VideoView) findViewById(R.id.video_view);
    mVideoView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.v("LOG_TAG, click");
        }
    });

However, when I run my application, the click event is never called.

So I wonder, is it impossible to register an OnClickListener on a VideoView? And, if so, why is that the case?

10条回答
姐就是有狂的资本
2楼-- · 2019-03-14 05:58

I know this is old but I used this:

    mVideoView.setOnTouchListener(new View.OnTouchListener()
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            Log.i(TAG, "Video 1 clicked, starting playback");

            return false;
        }
    });
查看更多
\"骚年 ilove
3楼-- · 2019-03-14 05:59

This is probably long overdue, nonetheless of some help to those who may run into a similar problem. The way I got around this problem was by laying a transparent image view right on top of the video view, then listening to onClick events on the image view, and doing whatever it was I wanted to do with the video view afterwards.

查看更多
疯言疯语
4楼-- · 2019-03-14 06:02

You can well use a button which is transparent on the video view if you want a specific part of the video on touch to do something.

查看更多
Bombasti
5楼-- · 2019-03-14 06:06

You might try Gesture Overlay View

You should be able to overlay this view on top of another view in order to get touch events.

Hope this helps!

查看更多
Ridiculous、
6楼-- · 2019-03-14 06:09

Here's how I solved the pause/play of VideoViews using onTouch:

// Class variables
private boolean bVideoIsBeingTouched = false;
private Handler mHandler = new Handler();

vvVideo.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
    if (!bVideoIsBeingTouched) {
        bVideoIsBeingTouched = true;
    if (vvVideo.isPlaying()) {
        vvVideo.pause();
    } else {
        vvVideo.resume();
    }
    mHandler.postDelayed(new Runnable() {
        public void run() {
            bVideoIsBeingTouched = false;
        }
        }, 100);
    }
    return true;
    }
});
查看更多
Animai°情兽
7楼-- · 2019-03-14 06:13

I know this is and old question, but here is what I did:

Since setOnClickListener is not been triggered, I created my own class which extends VideoView

public class VideoViewCustom extends VideoView{

and Overrided the onTouchEvent

@Override
public boolean onTouchEvent(MotionEvent ev) {

    if(ev.getAction() == MotionEvent.ACTION_DOWN){
        Log.d(TAG, "ACTION_DOWN");
    }

    return super.onTouchEvent(ev);
}

and now I can get the onClick event with the MotionEvent.

Hope this helps someone!

查看更多
登录 后发表回答