Android advanced tutorial (10)-Use of Android popupwindow!

Source: Internet
Author: User

Hello everyone, this section describes how to use Android popupwindow! In my understanding, popupwindow is actually similar to an immovable widget (only for the display effect !)

It is floating above other windows.

Below I will give you a simple demo, similar to the effect of the music player widget. When you click the button, popupwindow is displayed. Let's take a look at it first:

The following is the core code:

 

View plaincopy to clipboardprint?
  1. PackageCom. Android. tutor;
  2. ImportAndroid. App. activity;
  3. ImportAndroid. content. context;
  4. ImportAndroid. OS. Bundle;
  5. ImportAndroid. View. gravity;
  6. ImportAndroid. View. layoutinflater;
  7. ImportAndroid. View. view;
  8. ImportAndroid. View. View. onclicklistener;
  9. ImportAndroid. View. viewgroup. layoutparams;
  10. ImportAndroid. widget. Button;
  11. ImportAndroid. widget. popupwindow;
  12. Public ClassPopupwindowdemoExtendsActivityImplementsOnclicklistener {
  13. PrivateButton BTN;
  14. Public VoidOncreate (bundle savedinstancestate ){
  15. Super. Oncreate (savedinstancestate );
  16. Setcontentview (R. layout. Main );
  17. BTN = (button) findviewbyid (R. Id. BTN );
  18. BTN. setonclicklistener (This);
  19. }
  20. @ Override
  21. Public VoidOnclick (view v ){
  22. Context mcontext = popupwindowdemo.This;
  23. If(V. GETID () = R. Id. BTN ){
  24. Layoutinflater mlayoutinflater = (layoutinflater) mcontext
  25. . Getsystemservice (layout_inflater_service );
  26. View music_popunwindwow = mlayoutinflater. Inflate (
  27. R. layout. music_popwindow,Null);
  28. Popupwindow mpopupwindow =NewPopupwindow (music_popunwindwow, layoutparams. fill_parent,
  29. Layoutparams. wrap_content );
  30. Mpopupwindow. showatlocation (findviewbyid (R. Id. Main), gravity. Right | gravity. Bottom, 0, 0 );
  31. }
  32. }
  33. }

Package COM. android. tutor; <br/> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. OS. bundle; <br/> Import android. view. gravity; <br/> Import android. view. layoutinflater; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. view. viewgroup. layoutparams; <br/> Import android. widget. button; <br/> Import android. widget. popupwindow; <br/> public class popupwindowdemo extends activity implements onclicklistener {<br/> private button BTN; </P> <p> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> BTN = (button) findviewbyid (R. id. BTN); <br/> BTN. setonclicklistener (this); <br/>}< br/> @ override <br/> Public void onclick (view v) {<br/> context mcontext = popupwindowdemo. this; <br/> If (v. GETID () = R. id. BTN) {<br/> layoutinflater mlayoutinflater = (layoutinflater) mcontext <br/>. getsystemservice (layout_inflater_service); <br/> View music_popunwindwow = mlayoutinflater. inflate (<br/> r. layout. music_popwindow, null); <br/> popupwindow mpopupwindow = new popupwindow (music_popunwindwow, layoutparams. fill_parent, <br/> layoutparams. wrap_content); </P> <p> mpopupwindow. showatlocation (findviewbyid (R. id. main), gravity. right | gravity. bottom, 0, 0); <br/>}< br/>} 

 

It must be emphasized that the popupwindow must be triggered by an event. Otherwise, it will always be wrong. If you don't believe it, you can try it!

With the emergence of this problem, I will be able to learn it. So what should I do if I want to initialize and display the popupwindow? Instead of relying on other Click events,

Here I use the timer to achieve this effect. Of course handler will be used here. If you do not understand it, you can return

 

View plaincopy to clipboardprint?
  1. Android advanced tutorial (9) ---- Android handler !! Take a look and learn more:

Android advanced tutorial (9) ---- Android handler !! Take a look and learn more: 

 

The following is the core code:

 

View plaincopy to clipboardprint?
  1. PackageCom. Android. tutor;
  2. ImportJava. util. timer;
  3. ImportJava. util. timertask;
  4. ImportAndroid. App. activity;
  5. ImportAndroid. content. context;
  6. ImportAndroid. OS. Bundle;
  7. ImportAndroid. OS. Handler;
  8. ImportAndroid. OS. message;
  9. ImportAndroid. View. gravity;
  10. ImportAndroid. View. layoutinflater;
  11. ImportAndroid. View. view;
  12. ImportAndroid. View. viewgroup. layoutparams;
  13. ImportAndroid. widget. popupwindow;
  14. Public ClassPopupwindowdemoExtendsActivity {
  15. PrivateHandler mhandler =NewHandler (){
  16. Public VoidHandlemessage (Message MSG ){
  17. Switch(Msg. What ){
  18. Case1:
  19. Showpopupwindow ();
  20. Break;
  21. }
  22. };
  23. };
  24. Public VoidOncreate (bundle savedinstancestate ){
  25. Super. Oncreate (savedinstancestate );
  26. Setcontentview (R. layout. Main );
  27. // Create the timer
  28. Timer timer =NewTimer ();
  29. Timer. Schedule (NewInitpopupwindow (), 100 );
  30. }
  31. Private ClassInitpopupwindowExtendsTimertask {
  32. @ Override
  33. Public VoidRun (){
  34. Message message =NewMessage ();
  35. Message. What = 1;
  36. Mhandler. sendmessage (Message );
  37. }
  38. }
  39. Public VoidShowpopupwindow (){
  40. Context mcontext = popupwindowdemo.This;
  41. Layoutinflater mlayoutinflater = (layoutinflater) mcontext
  42. . Getsystemservice (layout_inflater_service );
  43. View music_popunwindwow = mlayoutinflater. Inflate (
  44. R. layout. music_popwindow,Null);
  45. Popupwindow mpopupwindow =NewPopupwindow (music_popunwindwow,
  46. Layoutparams. fill_parent, layoutparams. wrap_content );
  47. Mpopupwindow. showatlocation (findviewbyid (R. Id. Main), gravity. Center, 0, 0 );
  48. }
  49. }

Package COM. android. tutor; <br/> Import Java. util. timer; <br/> Import Java. util. timertask; <br/> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. OS. bundle; <br/> Import android. OS. handler; <br/> Import android. OS. message; <br/> Import android. view. gravity; <br/> Import android. view. layoutinflater; <br/> Import android. view. view; <br/> Import android. view. viewgroup. layoutparams; <br/> Import android. widget. popupwindow; <br/> public class popupwindowdemo extends activity {<br/> private handler mhandler = new handler () {</P> <p> Public void handlemessage (Message MSG) {<br/> switch (MSG. what) {<br/> case 1: <br/> showpopupwindow (); <br/> break; <br/>}< br/> }; <br/>}; </P> <p> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> // create the timer <br/> timer = new timer (); <br/> timer. schedule (New initpopupwindow (), 100 ); <br/>}</P> <p> private class initpopupwindow extends timertask {<br/> @ override <br/> Public void run () {</P> <p> message = new message (); <br/> message. what = 1; <br/> mhandler. sendmessage (Message); </P> <p >}< br/>}</P> <p> Public void showpopupwindow () {<br/> context mcontext = popupwindowdemo. this; <br/> layoutinflater mlayoutinflater = (layoutinflater) mcontext <br/>. getsystemservice (layout_inflater_service); <br/> View music_popunwindwow = mlayoutinflater. inflate (<br/> r. layout. music_popwindow, null); <br/> popupwindow mpopupwindow = new popupwindow (music_popunwindwow, <br/> layoutparams. fill_parent, layoutparams. wrap_content); <br/> mpopupwindow. showatlocation (findviewbyid (R. id. main), gravity. center, 0, 0); <br/>}< br/>} 

 

The effect is as follows:

In this way, you can initialize the popupwindow. Haha, there are a lot of layout files in this section. If you want the source code, leave your email and I will send it to you as soon as possible, today, we are here to leave a message that you do not understand !!! Thank you ~

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.