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:

 

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

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

 

The following is the core code:

 

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 if you do not understand anything !!! Thank you ~

 

Too many source codes are required. I am about to crash, so I uploaded them. : Http://d.download.csdn.net/down/2871531/Android_Tutor

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.