欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Android TV 原生VideoView抢占焦点问题

程序员文章站 2022-03-08 15:50:23
Android TV 原生VideoView抢占焦点问题在遥控操作的Android TV等产品中,打开含有原生VideoView的Activity时会出现焦点丢失,或者焦点在VideoView上的问题问题原因为在VideoView初始化中会进行焦点处理,导致无论是在xml中还是其它地方设置都无效// An highlighted block public VideoView(Context context, AttributeSet attrs, int defStyleAttr, int d...

Android TV 原生VideoView抢占焦点问题

在遥控操作的Android TV等产品中,打开含有原生VideoView的Activity时会出现焦点丢失,或者焦点在VideoView上的问题

问题原因为在VideoView初始化中会进行焦点处理,导致无论是在xml中还是其它地方设置都无效

// An highlighted block
    public VideoView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mVideoWidth = 0;
        mVideoHeight = 0;
        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
        mAudioAttributes = new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MOVIE).build();
        getHolder().addCallback(mSHCallback);
        getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		//start 初始化中对焦点的处理
        setFocusable(true);
        setFocusableInTouchMode(true);
        requestFocus();
		//end
        mCurrentState = STATE_IDLE;
        mTargetState = STATE_IDLE;
    }

解决方案:

public class MyVideoView extends VideoView {
    public MyVideoView(Context context) {
        super(context);
        initView();
    }

    public MyVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public MyVideoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public MyVideoView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView();
    }

    private void initView(){
        setFocusable(false);
        setFocusableInTouchMode(false);
        clearFocus();
    }
}

本文地址:https://blog.csdn.net/u012100033/article/details/111885816