Today, I want to achieve the effect of clicking the outside of Popupwindow and clicking the return key popupwindow to automatically close the window. I found it incomplete, fragmented, and inaccurate, So I thought about it myself, tried the effect. The following is the code for implementing the effect.
[Java]
View keyboardView = LayoutInflater. from (activity). inflate (R. layout. random_keyboard, null );
PopupWindow = new PopupWindow (keyboardView, LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT, false );
// Set the window to disappear when you click it.
PopupWindow. setOutsideTouchable (false );
// Set this parameter to get the focus; otherwise, you cannot click
PopupWindow. setFocusable (true );
// PopupWindow. setBackgroundDrawable (new BitmapDrawable (); // comment by danielinbiti, in addition, this line can be closed even outside the popupwindow.
KeyboardView. setFocusable (true); // comment by danielinbiti, set view to be able to answer the event, mark 1
KeyboardView. setFocusableInTouchMode (true); // comment by danielinbiti, set view to receive event annotation 2
KeyboardView. setOnKeyListener (new OnKeyListener (){
@ Override
Public boolean onKey (View arg0, int arg1, KeyEvent arg2 ){
If (arg1 = KeyEvent. KEYCODE_BACK ){
If (popupWindow! = Null ){
Close ();
}
}
Return false;
}
});
View keyboardView = LayoutInflater. from (activity). inflate (R. layout. random_keyboard, null );
PopupWindow = new PopupWindow (keyboardView, LayoutParams. WRAP_CONTENT, LayoutParams. WRAP_CONTENT, false );
// Set the window to disappear when you click it.
PopupWindow. setOutsideTouchable (false );
// Set this parameter to get the focus; otherwise, you cannot click
PopupWindow. setFocusable (true );
// PopupWindow. setBackgroundDrawable (new BitmapDrawable (); // comment by danielinbiti, in addition, this line can be closed even outside the popupwindow.
KeyboardView. setFocusable (true); // comment by danielinbiti, set view to be able to answer the event, mark 1
KeyboardView. setFocusableInTouchMode (true); // comment by danielinbiti, set view to receive event annotation 2
KeyboardView. setOnKeyListener (new OnKeyListener (){
@ Override
Public boolean onKey (View arg0, int arg1, KeyEvent arg2 ){
If (arg1 = KeyEvent. KEYCODE_BACK ){
If (popupWindow! = Null ){
Close ();
}
}
Return false;
}
});