【Andoid】自定义View中onKeyDown事件拦截不生效

需求:现在自定义的view中监听物理键
结果:onKeyDown方法根本没走,无法生效。
解决方法:创建View以后AddView之前执行如下方法

      if (context == null){
            if (SCUtils.getTopActivityInstance() == null){
                return;
            }
            screenCastControlView = new ScreenCastControlView(SCUtils.getTopActivityInstance());
        }else {
            screenCastControlView = new ScreenCastControlView(context);
        }
        screenCastControlView.setFocusable(true);
        screenCastControlView.setFocusableInTouchMode(true);
        screenCastControlView.requestFocus();
        screenCastControlView.requestFocusFromTouch();
        DyPluginBridgeUtils.addView(screenCastControlView);

实现自定义View

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import tv.douyu.plugin.demo.SCApplication;
import tv.douyu.plugin.screencast.R;

public class ScreenCastControlView extends LinearLayout implements View.OnClickListener {

    private Context mContext;
    private TextView tv_device_name;
    private TextView tv_wifi_name;

    private TextView definiton_select;
    private ExitDialog mExitDialog;
    private SCDefinitionSelectDialog definitionSelectDialog;

    public ScreenCastControlView(Context context) {
        super(context);
        this.mContext = context;
        initView();
    }

    public ScreenCastControlView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    public ScreenCastControlView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
    }

    private void initView() {

        View view = LayoutInflater.from(SCApplication.getInstance().getApplicationContext()).inflate(R.layout.view_screen_cast_control, this);

        TextView exit_cast = view.findViewById(R.id.exit_cast);
        exit_cast.setOnClickListener(this);
        LinearLayout ll_back = view.findViewById(R.id.ll_back);
        ll_back.setOnClickListener(this);



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.definiton_select:
                dialogList();
                break;
            default:
                break;
        }
    }

    private void dialogList() {

        if (definitionSelectDialog == null){
            definitionSelectDialog = new SCDefinitionSelectDialog(mContext);
        }
        definitionSelectDialog.show();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_VOLUME_UP:
                addMediaVolume();
                return true;
            case KeyEvent.KEYCODE_VOLUME_DOWN:
                cutMediaVolume();
                return true;
            default:
                break;
        }
        return super.onKeyDown(keyCode, event);

    }

错误不足之处或相关建议欢迎大家评论指出,谢谢!如果觉得内容可以的话麻烦喜欢(♥)一下

你可能感兴趣的:(【Andoid】自定义View中onKeyDown事件拦截不生效)