に発射ません。DispatchKeyEventは、私は私がこれを書いたように、のCustomViewで物理的なキーイベントをインターセプトする必要があります私のビュー
どうすればいいですか?
注:アクティビティで同じメソッドをオーバーライドしても動作しますが、私はCustomViewで実行する必要があります。
に発射ません。DispatchKeyEventは、私は私がこれを書いたように、のCustomViewで物理的なキーイベントをインターセプトする必要があります私のビュー
どうすればいいですか?
注:アクティビティで同じメソッドをオーバーライドしても動作しますが、私はCustomViewで実行する必要があります。
customView
がFocused
とFocusableInTouchMode = true
になると、KeyEvent
にキャッチすることができます。 DispatchEvent
ソースコードから、我々は、この見ることができます。そのため、customView
必見のに注目されて
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS))
== (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) {
// If this ViewGroup is focused and its size has been set(has border),pass the KeyEvent to the view
if (super.dispatchKeyEvent(event)) {
return true;
}
} else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS)
== PFLAG_HAS_BOUNDS) {
//If this ViewGroup has a focused child,and the child has a size
if (mFocused.dispatchKeyEvent(event)) {
return true;
// We can see that only the child is focused it can be catch the key event
}
}
if (mInputEventConsistencyVerifier != null) {
mInputEventConsistencyVerifier.onUnhandledEvent(event, 1);
}
return false;
}
をKeyEvent
、
をキャッチすることができ、私はあなたのcustomView
にこのコードを追加し、この問題を解決:
public GameView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
//Set whether this view can receive the focus
this.Focusable = true;
//When a view is focusable, it may not want to take focus when in touch mode.
//For example, a button would like focus when the user is navigating via a D-pad
//so that the user can click on it, but once the user starts touching the screen,
//the button shouldn't take focus
this.FocusableInTouchMode = true;
}
//Returns whether this View is able to take focus
public override bool IsFocused => true;
キーイベントがアクティビティによって処理されたため、ビューにパスできなかったようです。 –
あなたのカスタムビューにフォーカスが当たった後、KeyEventはアクティビティからCustomViewに渡されます。 –