Android之PopupWindow彈出對話方塊

來源:互聯網
上載者:User

Android的對話方塊常用的有兩種:PopupWindow和AlertDialog。

  1. popupWindow是一個阻塞式的彈出框,這就意味著在我們退出這個彈出框之前,程式會一直等待,,這就意味著在我們退出這個彈出框之前,程式會一直等待, 
  2.      *這和AlertDialog不同哦,AlertDialog是非阻塞式彈出框,AlertDialog彈出的時候,後台可是還可以做其他事情的哦。 

 

PopupWindow顧名思義為快顯功能表,不同於AlertDialog對話方塊,PopupWindow彈出的位置可以很多變化,按照有無位移分,可以分為無位移和位移兩種;按照參照類型不同又可以分為兩種:相對某個控制項(Anchor錨)的位置和父容器內部的相對位置。具體如下:

函數 簡介
showAsDropDown(View anchor) 相對某個控制項的位置(正左下方),無位移
showAsDropDown(View anchor, int xoff, int yoff) 相對某個控制項的位置,有位移(正數表示下方右邊,負數表示(上方左邊))
showAtLocation(View parent, int gravity, int x, int y) 父容器容器相對位置,例如正中央Gravity.CENTER,下方Gravity.BOTTOM等

 

下面是運行程式:

 

 

程式碼:

布局:main.xml

main.xml

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"      Android:id="@+id/layout"      Android:orientation="vertical"      Android:layout_width="fill_parent"      Android:layout_height="fill_parent"      >  <TextView        Android:id="@+id/tv_showText"      Android:layout_width="fill_parent"        Android:layout_height="wrap_content"        Android:gravity="center"      Android:text="@string/hello"      Android:textSize="22px"      />         <Button      Android:id="@+id/bt_PopupWindow1"      Android:text="以自己為Anchor,不位移"      Android:layout_width="fill_parent"        Android:layout_height="wrap_content"      />       <Button      Android:id="@+id/bt_PopupWindow2"      Android:text="以自己為Anchor,正下方"      Android:layout_width="fill_parent"        Android:layout_height="wrap_content"      />            <Button      Android:id="@+id/bt_PopupWindow3"      Android:text="以螢幕中心為參照,不位移(正中間)"      Android:layout_width="fill_parent"        Android:layout_height="wrap_content"      />       <Button      Android:id="@+id/bt_PopupWindow4"      Android:text="以螢幕下方為參照,下方中間"      Android:layout_width="fill_parent"        Android:layout_height="wrap_content"      />                 </LinearLayout>  

自訂對話方塊dialog.xml    

dialog.xml
<?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/tv_tip"      Android:layout_width="fill_parent"        Android:layout_height="wrap_content"        Android:text="請輸入內容:"      />        <EditText      Android:id="@+id/et_text"      Android:layout_width="fill_parent"        Android:layout_height="wrap_content"     ></EditText>        <LinearLayout      Android:gravity="center_horizontal"        Android:layout_width="fill_parent"      Android:layout_height="fill_parent"  >     <Button      Android:id="@+id/bt_ok"      Android:text="確定"      Android:layout_width="100px"        Android:layout_height="50px"      />       <Button      Android:id="@+id/bt_cancle"      Android:text="取消"      Android:layout_width="100px"        Android:layout_height="50px"      />     </LinearLayout>  </LinearLayout>  
import Android.app.Activity;   import Android.content.Context;   import Android.content.SharedPreferences.Editor;   import Android.os.Bundle;   import Android.util.Log;   import Android.view.Gravity;   import Android.view.LayoutInflater;   import Android.view.View;   import Android.view.View.OnClickListener;   import Android.view.ViewGroup.LayoutParams;   import Android.widget.Button;   import Android.widget.EditText;   import Android.widget.Gallery;   import Android.widget.PopupWindow;   import Android.widget.TextView;     public class PopupWindowTest extends Activity {//PopupWindow屬於不阻塞的對話方塊,AlertDialog則是阻塞的。       private Button bt_popupWindow1;       private Button bt_popupWindow2;       private Button bt_popupWindow3;       private Button bt_popupWindow4;       private TextView tv_showText;       private PopupWindow popupWindow;       private int screenWidth;       private int screenHeight;       private int dialgoWidth;       private int dialgoheight;                /** Called when the activity is first created. */      @Override      public void onCreate(Bundle savedInstanceState) {           super.onCreate(savedInstanceState);           setContentView(R.layout.main);                      initView();       }              /**       * 初始化控制項和響應事件       */      private void initView() {           bt_popupWindow1 = (Button)findViewById(R.id.bt_PopupWindow1);           bt_popupWindow2 = (Button)findViewById(R.id.bt_PopupWindow2);           bt_popupWindow3 = (Button)findViewById(R.id.bt_PopupWindow3);           bt_popupWindow4 = (Button)findViewById(R.id.bt_PopupWindow4);           tv_showText = (TextView)findViewById(R.id.tv_showText);                      bt_popupWindow1.setOnClickListener(new ClickEvent());           bt_popupWindow2.setOnClickListener(new ClickEvent());           bt_popupWindow3.setOnClickListener(new ClickEvent());           bt_popupWindow4.setOnClickListener(new ClickEvent());                                        }              /**       * 按鈕點擊事件處理       * @author Kobi       *       */      private class ClickEvent implements OnClickListener {                      @Override          public void onClick(View v) {               // TODO Auto-generated method stub               switch(v.getId()) {                                  case R.id.bt_PopupWindow1:  //以自己為Anchor,不位移                   getPopupWindow();                   popupWindow.showAsDropDown(v);                   break;                                  case R.id.bt_PopupWindow2:  //以自己為Anchor,位移(screenWidth-dialgoWidth)/2, 0)--按鈕正下方                   getPopupWindow();                   popupWindow.showAsDropDown(v, (screenWidth-dialgoWidth)/2, 0);                   break;                                  case R.id.bt_PopupWindow3:  //以螢幕中心為參照,不位移                   getPopupWindow();                   popupWindow.showAtLocation(findViewById(R.id.layout), Gravity.CENTER, 0, 0);                   break;                                  case R.id.bt_PopupWindow4:  //以螢幕左下角為參照,位移(screenWidth-dialgoWidth)/2, 0) --螢幕下方中央                   getPopupWindow();                   popupWindow.showAtLocation(findViewById(R.id.layout),                            Gravity.BOTTOM, 0, 0);                   break;                                  default:                   break;               }           }                  }         /**       * 建立PopupWindow       */      protected void initPopuptWindow() {           // TODO Auto-generated method stub                                 View popupWindow_view = getLayoutInflater().inflate(    //擷取自訂布局檔案dialog.xml的視圖                   R.layout.dialog, null,false);                      popupWindow = new PopupWindow(popupWindow_view, 200, 150, true);//建立PopupWindow執行個體                      Button bt_ok = (Button)popupWindow_view.findViewById(R.id.bt_ok);   //dialog.xml視圖裡面的控制項           Button bt_cancle = (Button)popupWindow_view.findViewById(R.id.bt_cancle);           final EditText et_text = (EditText)popupWindow_view.findViewById(R.id.et_text);                       bt_ok.setOnClickListener(new OnClickListener() {                              @Override              public void onClick(View v) {                   // TODO Auto-generated method stub                   tv_showText.setText(et_text.getText()); //在標籤裡顯示內容                   popupWindow.dismiss();                  //對話方塊消失               }           });                      bt_cancle.setOnClickListener(new OnClickListener() {                              @Override              public void onClick(View v) {                   // TODO Auto-generated method stub                   popupWindow.dismiss();               }           });                      //擷取螢幕和對話方塊各自高寬           screenWidth = PopupWindowTest.this.getWindowManager().getDefaultDisplay().getWidth();           screenHeight = PopupWindowTest.this.getWindowManager().getDefaultDisplay().getHeight();             dialgoWidth = popupWindow.getWidth();           dialgoheight = popupWindow.getHeight();                  }              /*       * 擷取PopupWindow執行個體       */      private void getPopupWindow() {                      if(null != popupWindow) {               popupWindow.dismiss();               return;           }else {               initPopuptWindow();           }       }         @Override      protected void onPause() {           // TODO Auto-generated method stub           super.onPause();           Log.e("ActivityState", "onPause");       }         @Override      protected void onResume() {           // TODO Auto-generated method stub           super.onResume();           Log.e("ActivityState", "onResume");       }                          }  

  

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.