編輯:Android 開發實例
談完 AlertDialog,接著來看另一種彈出式元件 PopupWindow。它與 AlertDialog 最大的差異在於外觀:PopupWindow 完全依附於你提供的 Layout,想要什麼樣子就寫什麼樣子,自由度極高;代價是必須自行呼叫 dismiss() 來關閉,不像 AlertDialog 有現成的按鈕與關閉行為。實務開發中,兩者經常搭配使用,各擅其職。
本例的主畫面只有一顆按鈕,點下去會在螢幕中央彈出一個登入框。按下「確定」會把輸入框自動填成預設帳密,按下「取消」則關閉視窗。
main.xml 的源碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="https://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演示"></Button>
- </LinearLayout>
popupwindow.xml 的源碼:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="https://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="用戶名"
- 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="密碼"
- 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="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>
- </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) 指定錨點與位置讓它顯示出來。
程式源碼:
- 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 when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //定義按鈕
- btnPopupWindow=(Button)this.findViewById(R.id.Button01);
- btnPopupWindow.setOnClickListener(new ClickEvent());
- }
- //統一處理按鍵事件
- class ClickEvent implements OnClickListener{
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(v==btnPopupWindow)
- {
- showPopupWindow(testAlertDialog.this,
- testAlertDialog.this.findViewById(R.id.Button01));
- }
- }
- }
- public void showPopupWindow(Context context,View parent){
- LayoutInflater inflater = (LayoutInflater)
- context.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 btnOK=(Button)vPopupWindow.findViewById(R.id.BtnOK);
- btnOK.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- //設置文本框內容
- EditText edtUsername=(EditText)vPopupWindow.findViewById(R.id.username_edit);
- edtUsername.setText("username");
- EditText edtPassword=(EditText)vPopupWindow.findViewById(R.id.password_edit);
- edtPassword.setText("password");
- }
- });
- //Cancel按鈕及其處理事件
- Button btnCancel=(Button)vPopupWindow.findViewById(R.id.BtnCancel);
- btnCancel.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- pw.dismiss();//關閉
- }
- });
- //顯示popupWindow對話框
- pw.showAtLocation(parent, Gravity.CENTER, 0, 0);
- }
- }
自訂 ProgressBar:用逐幀動畫做出載入中效果
系統內建的 ProgressBar 樣式有限,想要一個有個性的「載入中」動iveness,最簡單的做法就是用動畫——把幾張連續的圖片排成序列,讓它循環播放。這個技巧很早就
Android 理財 App 實作(四):新增帳單頁面的版面配置與 Spinner 資料綁定
這一節要把新增帳單的介面整理出來。動手前覺得應該不難,實際做下去才發現:排版面比寫程式還費工。上網找資料也收穫有限,官方範例裡用得最多的還是 ListView,但這個頁面
Android 讀取短信並轉存到 SQLite 的實現
系統短信本身也存在 SQLite 數據庫裡,但出於安全機制,Android 不允許其他應用直接訪問。繞過這個限制的正規做法是利用 ContentProvider:通過 c
Android 應用本地化指南:從 strings.xml 到多語系資源
一支上架到 Google Play 的 App,可能同時服務幾十個語系的使用者。要讓每個人都覺得「這 App 是為我做的」,就得把介面文字、數字格式與其他資源交給 And