惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 生如夏花之灿烂

本人讲课时录制的Android应用开发技术教学视频 jsoup使用样式class抓取数据时空格的处理 我的第一款anroid软件作品--短信精灵1.0 (原创)Android入门教程(三十六)------实现手机联系人的全选 Android入门教程(三十五)------在Android上使用ZXing识别条形码/二维码(转) - 生如夏花之灿烂 Android入门教程(三十四)------之多级树形菜单的实现 (转) Android入门教程(三十三)------之ListView自适应实现表格(转) Android入门教程(三十二)------之SQLite分页表格(转) Android入门教程(三十一)------SQLite分页读取(转) Android入门教程(三十)------之XML解析与生成(转) - 生如夏花之灿烂 Android入门教程(二十九)------之BroadcastReceiver (转) Android入门教程(二十八)------之Service(转) Android入门教程(二十七)------之Style与Theme (转) Android入门教程(二十六)------之ActivityGroup + GridView 实现Tab分页标签(转) Android入门教程(二十五)------之画图(转) Android入门教程(二十四)------之Gallery + ImageSwitcher(转) Android入门教程(二十三)------之Gallery(转) Android入门教程(二十二)------之TabHost,TabWidget(转) Android入门教程(二十)之--之AlertDialog(转)
Android入门教程(二十一)------之PopupWindow (转)
生如夏花之灿烂 · 2011-08-11 · via 博客园 - 生如夏花之灿烂

本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!    

       介绍过AlertDialog之后,接下来就介绍一下PopupWindow这种对话框。PopupWindow是阻塞对话框,只有在外部线程 或者 PopupWindow本身做退出操作才行。PopupWindow完全依赖Layout做外观,在常见的开发中,PopupWindow应该会与AlertDialog常混用。

       贴出本例中运行的结果图:

main.xml的源码如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://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弹出的截图,这里的PopupWindow是个登录框,点“确定”则自动填写,点“取消”则关闭PopupWindow。

popupwindow.xml的源码:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout xmlns:android="http://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>  

接下来是程序源码:

  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.       
  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.               
  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, nullfalse);  
  49.         final PopupWindow pw= new PopupWindow(vPopupWindow,300,300,true);  
  50.   
  51.           
  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.         
  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.           
  73.         pw.showAtLocation(parent, Gravity.CENTER, 00);  
  74.     }  
  75.       
  76. }