Learn Android<popupwindow pop-up windows from scratch. 34 .>

Source: Internet
Author: User

The Popupwindow component is a window that pops up on the screen after clicking a button or doing something, and makes UI updates, data processing, and so on.


Common methods

1 Public Popupwindow (Context context) Structure Creating an Popupwindow instance
2 Public Popupwindow (View contentview, int width, int height) Structure Create a Popupwindow instance, passing in the display width and height of the pop-up window
3 Public Popupwindow (View contentview, int width, int height, Boolean focusable) Structure Creates a Popupwindow instance, passing in the display width and height of the pop-up window, and whether to set focus
4 public void Dismiss () Ordinary Hide Window
5 public int getheight () Ordinary Get the height of the pop-up window
6 public int getwidth () Ordinary Get the width of the pop-up window
7 public boolean isshowing () Ordinary Determines whether the window is displayed
8 public void Setanimationstyle (int animationstyle) Ordinary Animating a pop-up window
9 public void Setcontentview (View contentview) Ordinary Set up Display components
10 public void Setfocusable (Boolean focusable) Ordinary Set whether to get focus
11 public void setheight (int height) Ordinary Set the pop-up window height
12 public void setwidth (int width) Ordinary Set the pop-up window width
13 public void Setondismisslistener (Popupwindow.ondismisslistener ondismisslistener) Ordinary Set the event listener after the pop-up window is hidden


Define two XML files, one is the default display, the other is the interface of the popup window.

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" wrap_content "android:layout_height=" Wrap_content " ><textviewandroid:id= "@+id/popinfo" android:text= "Please select your current status:" Android:layout_width= "Wrap_content" Android: Textsize= "20px" android:layout_height= "wrap_content"/><radiogroupandroid:id= "@+id/changestatus" Android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:orientation= "Vertical" Android: checkedbutton= "@+id/online" ><radiobuttonandroid:id= "@+id/online" android:text= "online"/>< Radiobuttonandroid:id= "@+id/offline" android:text= "Offline"/><radiobuttonandroid:id= "@+id/stealth" Android:text = "Stealth"/></radiogroup><button android:id= "@+id/cancel" android:layout_height= "Wrap_content" Android: Layout_width= "Wrap_content" android:text= "Cancel"/></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 " ><textview android:id= "@+id/statusinfo" android:layout_height= "wrap_content" android:layout_width= "Fill_ Parent "android:text=" Current User state: Online "/><button android:id=" @+id/popbut "android:layout_height=" Wrap_content " Android:layout_width= "Fill_parent" android:text= "status"/> </LinearLayout>


Java files


Package Com.example.popupwindow;import Android.app.activity;import Android.os.bundle;import android.view.Gravity; Import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.popupwindow;import Android.widget.radiobutton;import Android.widget.radiogroup;import Android.widget.radiogroup.oncheckedchangelistener;import Android.widget.textview;public class Mainactivity extends Activity {private button popbut = NULL;//button Component private Radiogrou P changestatus = null; Radio button Component Private TextView statusinfo = null; The text display component private Button cancel = null; Button component Private Popupwindow Popwin = null; Pop-up window private View popview = null; Save pop-up window layout @overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Super.setcontentview (R.layout.main); Set Layout manager this.popbut = (Button) Super.findviewbyid (r.id.popbut); Get Component This.statusinfo = (TextView) Super.findviewbyid (r.id.statusinfo); Get GroupPiece This.popbut.setOnClickListener (new Onclicklistenerimpl ()); Set Click event}private class Onclicklistenerimpl implements Onclicklistener {//Set listener @overridepublic void OnClick (view view) {L Ayoutinflater inflater = Layoutinflater.from (mainactivity.this);//Get Layoutinflater Object MainActivity.this.popView = Inflater.inflate (R.layout.popwindow, NULL);//Read layout manager MainActivity.this.popWin = new Popupwindow (Popview, 300, 220, true);//instantiation of PopupWindowMainActivity.this.changestatus = (radiogroup) Popview.findviewbyid (r.id.changestatus); Get Component MainActivity.this.cancel = (Button) Popview.findviewbyid (r.id.cancel); Get Component MainActivity.this.changestatus.setOnCheckedChangeListener (new Oncheckedchangelistenerimpl ());// Set Listener MainActivity.this.cancel.setOnClickListener (new Onclicklistener () {@Overridepublic void OnClick (View v) { MainActivity.this.popWin.dismiss (); Close pop-up}}); MainActivity.this.popWin.showAtLocation (MainActivity.this.popbut, gravity.center, 0, 0);//Display pop-up window}}private class Oncheckedchangelistenerimpl im@overridepublic void OnCheckedChanged (radiogroup group, int checkedid) is triggered when the Plementsoncheckedchangelistener {///option is selected) { RadioButton but = (RadioButton) MainActivity.this.popView.findViewById (Group.getcheckedradiobuttonid ());// Gets the selected component MainActivity.this.statusinfo.setText ("Current User state:" + but.gettext (). toString ());// Set text MainActivity.this.popWin.dismiss (); Close pop-up}}}





Android Basic Control Introduction Here is basically the end, there will be some other components, will be used in the time to give you a specific explanation




Learn Android<popupwindow pop-up windows from scratch. 34 .>

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.