Introduction to Android Popupwindow Usage Example Analysis _android

Source: Internet
Author: User

This example describes the Popupwindow dialog box. Popupwindow is a blocking dialog box that can only be executed if the external thread or Popupwindow itself does an exit operation. Popupwindow rely entirely on layout to do the appearance, in the common development, Popupwindow should be with alertdialog often mix.

First, post the result diagram that runs in this example:

Main.xml source code is as follows:

<?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"
  >
<button android:id= "@+id/button01" android:layout_height= "Wrap_content" Android:layout_width= "Fill_parent" android:text= "Popupwindow demo" ></Button>
</LinearLayout>

The following picture shows a screenshot of Popupwindow, where the Popupwindow is a login box, the point "OK" is automatically filled out, the point "Cancel" closes the Popupwindow.

Popupwindow.xml source code is as follows:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" wrap_content "android:orientation=" vertical " android:background= "#000000" > <textview android:id= "@+id/username_view android:layout_height=" wrap_content "Android:layout_marginleft=" 20dip "android:layout_marginright=" 20dip "android:text=" username "android:textappearance=" ? android:attr/textappearancemedium "android:layout_width=" fill_parent "/> <edittext android:id=" @+id/ Username_edit "android:layout_height=" wrap_content "android:layout_width=" Fill_parent "android:layout_marginLeft=" "20dip" android:layout_marginright= "20dip" android:capitalize= "None" android:textappearance= "? android:attr/
 Textappearancemedium "/> <textview android:id= @+id/password_view" android:layout_height= "Wrap_content" android:layout_marginleft= "20dip" android:layout_marginright= "20dip" android:text= "password" AndrOid:textappearance= "Android:attr/textappearancemedium" android:layout_width= "fill_parent"/> <EditText Android:id= "@+id/password_edit" android:layout_height= "wrap_content" android:layout_width= "Fill_parent" Android:
 layout_marginleft= "20dip" android:layout_marginright= "20dip" android:capitalize= "None" android:password= "true" Android:textappearance= "? Android:attr/textappearancemedium"/> <linearlayout android:id= "@+id/ LinearLayout01 "android:layout_height=" wrap_content "android:layout_width=" fill_parent "android:gravity=" "Center" ><button android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:id= "@+id/BtnOK" android:layout_weight= "android:text=" "OK" ></button><button android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_weight= "android:text=" "Cancel" android:id= "@+id/BtnCancel"

 ></Button></LinearLayout> </LinearLayout>

Next is the Java program source code:

Package com.testalertdialog;
Import android.app.Activity;
Import Android.app.AlertDialog;
Import Android.content.Context;
Import Android.content.DialogInterface;
Import Android.os.Bundle;
Import android.text.Editable;
Import android.view.Gravity;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.widget.Button;
Import Android.widget.EditText;


Import Android.widget.PopupWindow;
  public class Testalertdialog extends activity {Button Btnpopupwindow; /** called the activity is a.
    * * @Override public void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
    Setcontentview (R.layout.main);
    Define button btnpopupwindow= (buttons) This.findviewbyid (R.id.button01);
  Btnpopupwindow.setonclicklistener (New Clickevent ()); ///Unified Processing key event class Clickevent implements onclicklistener{@Override public void OnClick (View v) {//T
 Odo auto-generated Method stub  if (V==btnpopupwindow) {Showpopupwindow (Testalertdialog.this, TestAlertDialog.this.findViewById (r.id.button0
   1)); }} public void Showpopupwindow (context Context,view parent) {Layoutinflater Inflater = (layoutinflater) c  
   Ontext.getsystemservice (Context.layout_inflater_service);
   Final View vpopupwindow=inflater.inflate (R.layout.popupwindow, NULL, FALSE);

   Final Popupwindow pw= new Popupwindow (vpopupwindow,300,300,true);
    OK button and its Processing event button btnok= (button) Vpopupwindow.findviewbyid (R.id.btnok); Btnok.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {//Set text box content EditText edtus
    Ername= (EditText) Vpopupwindow.findviewbyid (R.id.username_edit);
    Edtusername.settext ("username");
    EditText edtpassword= (EditText) Vpopupwindow.findviewbyid (R.id.password_edit);
  Edtpassword.settext ("password");
    
   }
    });
   Cancel button and its Processing event button btncancel= (button) Vpopupwindow.findviewbyid (R.id.btncancel); Btncancel.setonclicklistener (New Onclicklistener () {@Override public void OnClick (View v) {Pw.dismiss ();//Close}
    });
 Displays the Popupwindow dialog box Pw.showatlocation (parent, gravity.center, 0, 0); }
}

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.