当我的对话片段hide
时,dispatchKeyEvent
工作正常
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
Toast.makeText(FragmentPlayer.this, "test: called", Toast.LENGTH_SHORT).show();
return super.dispatchKeyEvent(event);
}
但是当我的对话片段是
show
时,dispatchKeyEvent
不被调用MyDialogFragment mFragment = new MyDialogFragment();
mFragment.show(getSupportFragmentManager(), "MyDialog");
为什么?
最佳答案
无需将DialogFragment
代码更改为Dialog
,您可以这样做(以防仍然需要)。使用OnKeyListener
将解决您的问题。
public class BaseDialogFragment extends AppCompatDialogFragment {
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
/* Your logic, you get the KeyEvent*/
return false;
}
});
}