Android popupwindow click back to close

Source: Internet
Author: User

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;
}
});

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.