Why is View dispatchKeyEvent not called?

In my activity, ShowcaseView will appear, which will add to the decor view ((ViewGroup) activity.getWindow().getDecorView()).addView(mShowcaseView); , and I want to define a key event to handle something, so I redefine dispatchKeyEvent()it to do what I need. but it looks like the method dispatchKeyEvent()has never been called, which is worse, PhoneWindow.DecorView#dispatchKeyEvent()not called, I don’t know why, please help me.

Thanks:).

here is my short ShowcaseViewsource code.

public class ShowcaseView extends FrameLayout {
    public ShowcaseView(Context context) {
        super(context);
        init(context);
    }

    public ShowcaseView(Context context, AttributeSet attrs) {
       super(context, attrs);
       init(context);
    }

    public ShowcaseView(Context context, AttributeSet attrs, int defStyleAttr) {
       super(context, attrs, defStyleAttr);
       init(context);
    }

    private void init(Context context) {
        setWillNotDraw(false);
        setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
        setFocusable(true);
        setEnabled(true);
    }

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    Log.d("Showcase", "dispatchKeyEvent: called");
    super.dispatchKeyEvent(event);
    return executeKeyEvent(event);
}

private boolean executeKeyEvent(KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_UP
            && event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
        hide();
    }
    return true;
}
}
+4
source share
1 answer

According to https://developer.android.com/training/keyboard-input/commands.html :

. KeyEvent API , . ( ).

textWatcher editText.

editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

.

0

Source: https://habr.com/ru/post/1618842/


All Articles