Use and extension of popupwindow

Source: Internet
Author: User

Popupwindow

 

[Function]

Popupwindow is a kind of user reminder, and its overhead is smaller than activity.

 

 

[Code steps]

1. Define the layout for popupwindow, for example, hello. xml

 

Java code
  1. <? XML version =
    "1.0"
    Encoding =
    "UTF-8"
    ?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"

  3. Android: Orientation = "horizontal"

  4. Android: layout_width = "fill_parent"

  5. Android: layout_height = "fill_parent"

  6. Android: padding = "10dp"

  7. >
  8. <Imageview
  9. Android: Id = "@ + ID/image"

  10. Android: layout_width = "wrap_content"

  11. Android: layout_height = "wrap_content"

  12. Android: src = "@ drawable/robot"
    />
  13. <Linearlayout
  14. Android: Orientation = "vertical"

  15. Android: layout_width = "wrap_content"

  16. Android: layout_height = "wrap_content"

  17. Android: paddingleft = "20dip"

  18. >
  19. <Textview
  20. Android: layout_width = "wrap_content"

  21. Android: layout_height = "wrap_content"

  22. Android: text = "hellopop! "

  23. />
  24. <Button
  25. Android: Id = "@ + ID/hellobutton"

  26. Android: layout_width = "100dip"

  27. Android: layout_height = "wrap_content"

  28. Android: text = "OK"

  29. />
  30. </Linearlayout>
  31. </Linearlayout>
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "horizontal" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: padding = "10dp"> <imageview Android: Id = "@ + ID/image" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/robot"/> <linearlayout Android: Orientation = "vertical" Android: Layout _ Width = "wrap_content" Android: layout_height = "wrap_content" Android: paddingleft = "20dip"> <textview Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: TEXT = "hellopop! "/> <Button Android: Id =" @ + ID/hellobutton "Android: layout_width =" 100dip "Android: layout_height =" wrap_content "Android: TEXT = "OK"/> </linearlayout>

 

 

2. Use layoutinflater to get the view of Hello. xml

Java code
  1. View =
    This
    . Getlayoutinflater (). Inflate (R. layout. Hello,
    Null
    );
View = This. getlayoutinflater (). Inflate (R. layout. Hello, null );

 

3. Create popupwindow pop and use the above layout File View

Java code
  1. Pop =
    New
    Popupwindow (view,
    500
    ,
    200
    );
Pop = new popupwindow (view, 500,200 );

 

4. Pop-up popupwindow

* Layout definition file: Main. xml includes a button

Java code
  1. <? XML version =
    "1.0"
    Encoding =
    "UTF-8"
    ?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"

  3. Android: Orientation = "vertical"

  4. Android: Id = "@ + ID/main"

  5. Android: layout_width = "fill_parent"

  6. Android: layout_height = "fill_parent"

  7. >
  8. <Textview
  9. Android: layout_width = "fill_parent"

  10. Android: layout_height = "wrap_content"

  11. Android: text = "Pop demo! "

  12. />
  13. <Button
  14. Android: Id = "@ + ID/button"

  15. Android: layout_width = "fill_parent"

  16. Android: layout_height = "wrap_content"

  17. Android: text = "to pop! "

  18. />
  19. </Linearlayout>
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: Id = "@ + ID/main" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <textview Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: text = "Pop demo! "/> <Button Android: Id =" @ + ID/button "Android: layout_width =" fill_parent "Android: layout_height =" wrap_content "Android: text =" to pop! "/> </Linearlayout>

 

 

* Pop-up: There are two modes: one is the drop-down mode and the other is the specified location.

-Drop-down list:

Java code
  1. Findviewbyid (R. Id. Button). setonclicklistener (
    New
    View. onclicklistener (){
  2. Public
     
    Void
    Onclick (view v ){
  3. // Todo auto-generated method stub

  4. Pop. showasdropdown (v );
  5. }
  6. });
Findviewbyid (R. id. button ). setonclicklistener (new view. onclicklistener () {public void onclick (view v) {// todo auto-generated method stub pop. showasdropdown (v );}});

 

 

-Specified location:

Java code
  1. Findviewbyid (R. Id. Button). setonclicklistener (
    New
    View. onclicklistener (){
  2. Public
     
    Void
    Onclick (view v ){
  3. // Todo auto-generated method stub

  4. Pop. showatlocation (findviewbyid (R. Id. Main), gravity. Center, 20
    ,
    20
    );
  5. }
  6. });
Findviewbyid (R. id. button ). setonclicklistener (new view. onclicklistener () {public void onclick (view v) {// todo auto-generated method stub pop. showatlocation (findviewbyid (R. id. main), gravity. center, 20, 20 );}});

 

 

5. Cancel

Java code
  1. View. findviewbyid (R. Id. hellobutton). setonclicklistener (
    New
    View. onclicklistener (){
  2. Public
     
    Void
    Onclick (view v ){
  3. // Todo auto-generated method stub

  4. Pop. Dismiss ();
  5. }
  6. });
View. findviewbyid (R. id. hellobutton ). setonclicklistener (new view. onclicklistener () {public void onclick (view v) {// todo auto-generated method stub pop. dismiss ();}});

 

6. Other problems:

* I found that many people have doubts about which item is clicked after the listview is included in the popupwindow. So I tested it and found that it is the same as the general method. There is nothing special about it. share

 

It is written that because listview is expanded and displayed, it will lead to poor appearance, so take the spinner as an example.

 

 

6.1. Define the layout file "Hello. xml" containing the spinner

Java code
  1. <? XML version =
    "1.0"
    Encoding =
    "UTF-8"
    ?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"

  3. Android: Orientation = "vertical"

  4. Android: layout_width = "fill_parent"

  5. Android: layout_height = "fill_parent"

  6. >
  7. <Linearlayout
  8. Android: Orientation = "horizontal"

  9. Android: layout_width = "fill_parent"

  10. Android: layout_height = "wrap_content"

  11. >
  12. <Imageview
  13. Android: Id = "@ + ID/image"

  14. Android: layout_width = "wrap_content"

  15. Android: layout_height = "wrap_content"

  16. Android: src = "@ drawable/robot"
    />
  17. <Textview
  18. Android: layout_width = "wrap_content"

  19. Android: layout_height = "wrap_content"

  20. Android: text = "hellopop! "

  21. />
  22. </Linearlayout>
  23. <Spinner
  24. Android: Id = "@ + ID/Spinner"

  25. Android: layout_width = "wrap_content"

  26. Android: layout_height = "40dip"
    />
  27. </Linearlayout>
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <linearlayout Android: orientation = "horizontal" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content"> <imageview Android: Id = "@ + ID/image" Android: layout_width = "wrap_content" Android: layout_height = "wrap_conten T "Android: src =" @ drawable/robot "/> <textview Android: layout_width =" wrap_content "Android: layout_height =" wrap_content "Android: text =" hellopop! "/> </Linearlayout> <spinner Android: Id =" @ + ID/Spinner "Android: layout_width =" wrap_content "Android: layout_height =" 40dip "/> </linearlayout>

 

6.2. Get the instance of the spinner: Spinner

Java code
  1. Spinner = (spinner) view. findviewbyid (R. Id. spinner );
Spinner = (spinner) view. findviewbyid (R. Id. spinner );

 

6.3. Bind a spinner with specific data. In this example, contact is used as an example.

Java code
  1. Public
     
    Void
    Specifyspinner (){
  2. Cursor c = getcontentresolver (). Query (people. content_uri,
  3. Null
    ,
    Null
    ,
    Null
    ,
    Null
    );
  4. Simplecursoradapter adapter = new
    Simplecursoradapter (
    This
    ,
  5. Android. R. layout. simple_list_item_1, C,
  6. New
    String [] {People. name },
  7. New
     
    Int
    [] {Android. R. Id. text1 });
  8. Adapter. setdropdownviewresource (
  9. Android. R. layout. simple_spinner_dropdown_item );
  10. Spinner. setadapter (adapter );
  11. }
Public void specifyspinner () {cursor c = getcontentresolver (). query (people. content_uri, null, null); simplecursoradapter adapter = new simplecursoradapter (this, android. r. layout. simple_list_item_1, C, new string [] {people. name}, new int [] {android. r. id. text1}); adapter. setdropdownviewresource (Android. r. layout. simple_spinner_dropdown_item); spinner. setadapter (adapter );}

 

Don't forget the contact's access permission:

<Uses-Permission Android: Name = "android. Permission. read_contacts"/>

 

6.4. Obtain specific items:

Java code
  1. Spinner. setonitemselectedlistener (
    New
    Onitemselectedlistener (){
  2. Public
     
    Void
    Onitemselected (adapterview <?> Adapter, view V,
  3. Int
    POs,
    Long
    ID ){
  4. Updatetitle (POS );
  5. }
  6. Public
     
    Void
    Onnothingselected (adapterview <?> Arg0 ){
  7. // Todo auto-generated method stub

  8. }
  9. });
Spinner. setonitemselectedlistener (New onitemselectedlistener () {public void onitemselected (adapterview <?> Adapter, view V, int POs, long ID) {updatetitle (POS);} public void onnothingselected (adapterview <?> Arg0) {// todo auto-generated method stub }});

 

Write updatetitle (INT) to display the position in the title

Public void updatetitle (int I ){
This. settitle ("hellopop:" + I );
}

 

 

6.5. emulator run:

 

Http://www.iteye.com/topic/604462

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.