How to Ignore Search Key Pressing in the Dialog Box

I previously extended the class Dialogfor my dialogs, and I had to use the following code segment in my dialogs so that they would not be fired when I press the search key on the phone:

setOnKeyListener(new OnKeyListener()
{
  public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
  {
    if (keyCode == KeyEvent.KEYCODE_SEARCH)
      return true; // pretend we've processed it
    else
      return false; // pass on to be processed as normal
  }
});

Now I use the class DialogFragmentfor my dialogs. Unfortunately, it DialogFragmenthas the same problem as Dialogin this dialog they are rejected by pressing the phone search key (regardless of the status of canceling the dialog). The two-sided problem is that it DialogFragmentdoes not have a method setOnKeyListener, so the above code segment is no longer applied.

Does anyone know how I can get my dialogs (when shown) to ignore search key presses?

. onKeyUp, , , onKeyUp DialogFragment. , , DialogFragment .

+5
1

. DialogFragment.Onresume

 getDialog().setOnKeyListener(new OnKeyListener()
 {
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event){
    if (keyCode == KeyEvent.KEYCODE_SEARCH)
      return true; // pretend we've processed it
    else
      return false; // pass on to be processed as normal
  }
});
+14

All Articles