Android入門之PopupWindow用法實(shí)例解析
本文實(shí)例介紹一下PopupWindow對話框。PopupWindow是阻塞對話框,只有在外部線程 或者 PopupWindow本身做退出操作才可以執(zhí)行。PopupWindow完全依賴Layout做外觀,在常見的開發(fā)中,PopupWindow應(yīng)該會與AlertDialog常混用。
先貼出本例中運(yùn)行的結(jié)果圖:

main.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" > <Button android:id="@+id/Button01" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="PopupWindow演示"></Button> </LinearLayout>
下圖所示是PopupWindow彈出的截圖,這里的PopupWindow是個登錄框,點(diǎn)“確定”則自動填寫,點(diǎn)“取消”則關(guān)閉PopupWindow。

popupwindow.xml的源碼如下:
<?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="用戶名" 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>
接下來是Java程序源碼:
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());
}
//統(tǒng)一處理按鍵事件
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) {
//設(shè)置文本框內(nèi)容
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();//關(guān)閉
}
});
//顯示popupWindow對話框
pw.showAtLocation(parent, Gravity.CENTER, 0, 0);
}
}
- android PopupWindow 和 Activity彈出窗口實(shí)現(xiàn)方式
- Android Animation實(shí)戰(zhàn)之屏幕底部彈出PopupWindow
- android popwindow實(shí)現(xiàn)左側(cè)彈出菜單層及PopupWindow主要方法介紹
- Android編程實(shí)現(xiàn)popupwindow彈出后屏幕背景變成半透明效果
- Android實(shí)現(xiàn)底部彈出PopupWindow背景逐漸變暗效果
- Android中PopupWindow響應(yīng)返回鍵并關(guān)閉的2種方法
- android使用PopupWindow實(shí)現(xiàn)頁面點(diǎn)擊頂部彈出下拉菜單
- Android之用PopupWindow實(shí)現(xiàn)彈出菜單的方法詳解
- Android PopupWindow實(shí)現(xiàn)右側(cè)、左側(cè)和底部彈出菜單
- Android簡單使用PopupWindow的方法
相關(guān)文章
Android編程實(shí)現(xiàn)將壓縮數(shù)據(jù)庫文件拷貝到安裝目錄的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)將壓縮數(shù)據(jù)庫文件拷貝到安裝目錄的方法,涉及Android處理壓縮文件的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
Android入門之讀寫本地文件的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android如何實(shí)現(xiàn)讀寫本地文件的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Android有一定的幫助,需要的可以參考一下2022-12-12
Android開發(fā)中記一個SwipeMenuListView側(cè)滑刪除錯亂的Bug
這篇文章主要介紹了Android開發(fā)中記一個SwipeMenuListView側(cè)滑刪除錯亂的Bug的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-09-09
android自定義view實(shí)現(xiàn)推箱子小游戲
這篇文章主要為大家詳細(xì)介紹了android自定義view實(shí)現(xiàn)推箱子小游戲,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
Android開發(fā)之瀑布流控件的實(shí)現(xiàn)與使用方法示例
這篇文章主要介紹了Android開發(fā)之瀑布流控件的實(shí)現(xiàn)與使用方法,結(jié)合實(shí)例形式分析了Android瀑布流控件的定義與使用方法,需要的朋友可以參考下2017-10-10
Android中Java根據(jù)文件頭獲取文件類型的方法
這篇文章主要介紹了Android中Java根據(jù)文件頭獲取文件類型的方法,涉及Android針對文件屬性的相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
Android顯示系統(tǒng)SurfaceFlinger詳解
本文詳細(xì)講解了Android顯示系統(tǒng)SurfaceFlinger,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12
基于Flutter實(shí)現(xiàn)動態(tài)高斯模糊的流程步驟
一個App加上高斯模糊會形成一種高級的感覺,本文將介紹如何制作一個根據(jù)背景內(nèi)容來動態(tài)高斯模糊,文中有詳細(xì)的代碼實(shí)現(xiàn)步驟,代碼示例講解的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下2023-11-11

