Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 入門第十篇:用 PopupWindow 做自訂彈出視窗

編輯:Android 開發實例

談完 AlertDialog,接著來看另一種彈出式元件 PopupWindow。它與 AlertDialog 最大的差異在於外觀:PopupWindow 完全依附於你提供的 Layout,想要什麼樣子就寫什麼樣子,自由度極高;代價是必須自行呼叫 dismiss() 來關閉,不像 AlertDialog 有現成的按鈕與關閉行為。實務開發中,兩者經常搭配使用,各擅其職。

本例的主畫面只有一顆按鈕,點下去會在螢幕中央彈出一個登入框。按下「確定」會把輸入框自動填成預設帳密,按下「取消」則關閉視窗。

main.xml 的源碼如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <Button android:id="@+id/Button01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="PopupWindow演示"></Button> 
  8. </LinearLayout> 

popupwindow.xml 的源碼:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2.  
  3. <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
  4.     android:layout_width="fill_parent" android:layout_height="wrap_content" 
  5.     android:orientation="vertical" android:background="#000000"> 
  6.  
  7.     <TextView android:id="@+id/username_view" 
  8.         android:layout_height="wrap_content" 
  9.         android:layout_marginLeft="20dip" 
  10.         android:layout_marginRight="20dip" android:text="用戶名" 
  11.         android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="fill_parent"/> 
  12.  
  13.     <EditText android:id="@+id/username_edit" 
  14.         android:layout_height="wrap_content" 
  15.         android:layout_width="fill_parent" android:layout_marginLeft="20dip" 
  16.         android:layout_marginRight="20dip" android:capitalize="none" 
  17.         android:textAppearance="?android:attr/textAppearanceMedium" /> 
  18.  
  19.     <TextView android:id="@+id/password_view" 
  20.         android:layout_height="wrap_content" 
  21.         android:layout_marginLeft="20dip" 
  22.         android:layout_marginRight="20dip" android:text="密碼" 
  23.         android:textAppearance="?android:attr/textAppearanceMedium" android:layout_width="fill_parent"/> 
  24.  
  25.     <EditText android:id="@+id/password_edit" 
  26.         android:layout_height="wrap_content" 
  27.         android:layout_width="fill_parent" android:layout_marginLeft="20dip" 
  28.         android:layout_marginRight="20dip" android:capitalize="none" 
  29.         android:password="true" 
  30.         android:textAppearance="?android:attr/textAppearanceMedium" /> 
  31.  
  32. <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="100" android:text="確定"></Button><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="100" android:text="取消" android:id="@+id/BtnCancel"></Button></LinearLayout> 
  33. </LinearLayout> 

實作上的三個關鍵

用 LayoutInflater 把 XML 變成 View。PopupWindow 要顯示的內容是一份獨立配置檔,得先透過 getSystemService(Context.LAYOUT_INFLATER_SERVICE) 取得 inflater,再用 inflate 產生 View 物件。

建構時就決定尺寸與是否可焦。new PopupWindow(vPopupWindow, 300, 300, true) 四個參數分別是內容 View、寬、高,以及是否取得焦點。第三個參數設為 true,彈出視窗才會攔截事件、 behaves 像個真正的對話框。

子元件的事件要從 popup 的 View 上找。由於 popupwindow.xml 是獨立的一份 View 樹,findViewById 必須對著 vPopupWindow 呼叫,而不是 Activity。確定鈕裡抓出帳號與密碼輸入框寫入內容,取消鈕則呼叫 pw.dismiss() 關閉。最後以 showAtLocation(parent, Gravity.CENTER, 0, 0) 指定錨點與位置讓它顯示出來。

程式源碼:

 
  1. package com.testAlertDialog;     
  2.     
  3. import android.app.Activity;     
  4. import android.app.AlertDialog;     
  5. import android.content.Context;     
  6. import android.content.DialogInterface;     
  7. import android.os.Bundle;     
  8. import android.text.Editable;     
  9. import android.view.Gravity;     
  10. import android.view.LayoutInflater;     
  11. import android.view.View;     
  12. import android.view.View.OnClickListener;     
  13. import android.widget.Button;     
  14. import android.widget.EditText;     
  15. import android.widget.PopupWindow;     
  16.     
  17.     
  18. public class testAlertDialog extends Activity {     
  19.     Button btnPopupWindow;     
  20.     /** Called when the activity is first created. */    
  21.     @Override    
  22.     public void onCreate(Bundle savedInstanceState) {     
  23.         super.onCreate(savedInstanceState);     
  24.         setContentView(R.layout.main);     
  25.         //定義按鈕     
  26.         btnPopupWindow=(Button)this.findViewById(R.id.Button01);     
  27.         btnPopupWindow.setOnClickListener(new ClickEvent());     
  28.     }     
  29.           
  30.           
  31.     //統一處理按鍵事件     
  32.     class ClickEvent implements OnClickListener{     
  33.     
  34.         @Override    
  35.         public void onClick(View v) {     
  36.             // TODO Auto-generated method stub     
  37.             if(v==btnPopupWindow)     
  38.             {     
  39.                 showPopupWindow(testAlertDialog.this,     
  40.                         testAlertDialog.this.findViewById(R.id.Button01));     
  41.             }     
  42.         }     
  43.     }     
  44.     
  45.     public void showPopupWindow(Context context,View parent){     
  46.         LayoutInflater inflater = (LayoutInflater)        
  47.         context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        
  48.         final View vPopupWindow=inflater.inflate(R.layout.popupwindow, null, false);     
  49.         final PopupWindow pw= new PopupWindow(vPopupWindow,300,300,true);     
  50.     
  51.         //OK按鈕及其處理事件     
  52.         Button btnOK=(Button)vPopupWindow.findViewById(R.id.BtnOK);     
  53.         btnOK.setOnClickListener(new OnClickListener(){     
  54.             @Override    
  55.             public void onClick(View v) {     
  56.                 //設置文本框內容     
  57.                 EditText edtUsername=(EditText)vPopupWindow.findViewById(R.id.username_edit);     
  58.                 edtUsername.setText("username");     
  59.                 EditText edtPassword=(EditText)vPopupWindow.findViewById(R.id.password_edit);     
  60.                 edtPassword.setText("password");     
  61.             }     
  62.         });     
  63.              
  64.         //Cancel按鈕及其處理事件     
  65.         Button btnCancel=(Button)vPopupWindow.findViewById(R.id.BtnCancel);     
  66.         btnCancel.setOnClickListener(new OnClickListener(){     
  67.             @Override    
  68.             public void onClick(View v) {     
  69.                 pw.dismiss();//關閉     
  70.             }     
  71.         });     
  72.         //顯示popupWindow對話框     
  73.         pw.showAtLocation(parent, Gravity.CENTER, 0, 0);     
  74.     }     
  75.           
  76. } 
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved