【Android开发学习33】PopupWindow之显示顶层对话框

一、基础知识:

 

1. PopupWindow是阻塞对话框,只有在外部线程 或者 PopupWindow本身做退出操作才行。

2. PopupWindow完全依赖Layout做外观

 

 

 

二、使用方法:

1. 为我们的对话框新建一个Layout文件作为外观:(下面是我的一个示例)

[html]  view plain copy
  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"   
  5.     android:layout_height="wrap_content"    
  6.     android:orientation="vertical"  
  7.     android:background="#808080"   
  8.     >    
  9.     <ImageButton   
  10.         android:id="@+id/username_image"  
  11.         android:layout_x="@string/username_image_x"  
  12.         android:layout_y="@string/username_image_y"  
  13.         android:layout_width="wrap_content"   
  14.         android:layout_height="wrap_content" />  
  15.     <EditText android:id="@+id/username_edit"    
  16.         android:layout_height="wrap_content"    
  17.         android:layout_width="fill_parent"   
  18.         android:layout_marginLeft="20dip"    
  19.         android:layout_marginRight="20dip"   
  20.         android:capitalize="none"    
  21.         android:textAppearance="?android:attr/textAppearanceMedium" />    
  22.     <TextView android:id="@+id/password_view"    
  23.         android:layout_height="wrap_content"    
  24.         android:layout_marginLeft="20dip"    
  25.         android:layout_marginRight="20dip"   
  26.         android:text="密码"    
  27.         android:textAppearance="?android:attr/textAppearanceMedium"   
  28.         android:layout_width="fill_parent"/>    
  29.     <EditText android:id="@+id/password_edit"    
  30.         android:layout_height="wrap_content"    
  31.         android:layout_width="fill_parent"   
  32.         android:layout_marginLeft="20dip"    
  33.         android:layout_marginRight="20dip"   
  34.         android:capitalize="none"    
  35.         android:password="true"    
  36.         android:textAppearance="?android:attr/textAppearanceMedium" />    
  37.     
  38.     <LinearLayout   
  39.         android:id="@+id/LinearLayout01"   
  40.         android:layout_height="wrap_content"   
  41.         android:layout_width="fill_parent"   
  42.         android:gravity="center">  
  43.         <Button   
  44.             android:layout_width="wrap_content"   
  45.             android:layout_height="wrap_content"   
  46.             android:id="@+id/BtnOK"   
  47.             android:layout_weight="100"   
  48.             android:text="确定">  
  49.         </Button>  
  50.         <Button   
  51.             android:layout_width="wrap_content"   
  52.             android:layout_height="wrap_content"   
  53.             android:layout_weight="100"   
  54.             android:text="取消"   
  55.             android:id="@+id/BtnCancel">       
  56.         </Button>  
  57.     </LinearLayout>    
  58.       
  59. </LinearLayout>    


 

2. 为我们的对话框添加效果显示需要的文件:(我的Demo里面是一组XML文件)

(具体代码,详见代码-->> 文章最后给出下载地址)

 

3. 在主程序中,构造我们的对话框:

[java]  view plain copy
  1. public void showPopupWindow(Context context,View parent){  
  2.         /*** ==== Start 创建popupWindow对话框 ==== ***/     
  3.         LayoutInflater inflater = (LayoutInflater)       
  4.            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);       
  5.         final View vPopupWindow=inflater.inflate(R.layout.popupwindow, nullfalse);    
  6.           
  7.         final PopupWindow pw= new PopupWindow(vPopupWindow,300,300,true);    
  8.         /*** ==== End 创建popupWindow对话框 ==== ***/  
  9.           
  10.         /*** ==== Start 设置popupWindow对话框 上面的按钮显示及响应  ==== ***/    
  11.         ImageButton loginImageButton = (ImageButton)vPopupWindow.findViewById(R.id.username_image);  
  12.         loginImageButton.setBackgroundResource(R.drawable.login);  
  13.           
  14.         //OK按钮及其处理事件     
  15.         Button btnOK=(Button)vPopupWindow.findViewById(R.id.BtnOK);    
  16.         btnOK.setOnClickListener(new OnClickListener(){    
  17.             @Override    
  18.             public void onClick(View v) {    
  19.                 //设置文本框内容     
  20.                 EditText edtUsername=(EditText)vPopupWindow.findViewById(R.id.username_edit);    
  21.                 edtUsername.setText("username");    
  22.                 EditText edtPassword=(EditText)vPopupWindow.findViewById(R.id.password_edit);    
  23.                 edtPassword.setText("password");    
  24.             }    
  25.         });    
  26.             
  27.         //Cancel按钮及其处理事件     
  28.         Button btnCancel=(Button)vPopupWindow.findViewById(R.id.BtnCancel);    
  29.         btnCancel.setOnClickListener(new OnClickListener(){    
  30.             @Override    
  31.             public void onClick(View v) {    
  32.                 pw.dismiss();//关闭     
  33.             }    
  34.         });    
  35.         /*** ==== End 设置popupWindow对话框 上面的按钮显示及响应  ==== ***/  
  36.           
  37.         /*** ==== Start 显示popupWindow对话框 ==== ***/     
  38.         //设置SelectPicPopupWindow弹出窗体动画效果     
  39.         pw.setAnimationStyle(R.style.FromRightAnimation);   // 从屏幕右边移进来  
  40.         //pw.setAnimationStyle(R.style.PopupAnimation);     // 从小放大  
  41.         // 指定PopupWindow的显示位置  
  42.         //pw.showAtLocation(vPopupWindow,0,20,100);  
  43.         // 显示在屏幕的最中央  
  44.         pw.showAtLocation(parent, Gravity.CENTER, 00);  
  45.         pw.update();  
  46.         /*** ==== End 显示popupWindow对话框 ==== ***/  
  47.     }    

 


 

三、效果展示:

 

 

 

四、代码下载地址:http://download.csdn.net/detail/ypist/5229389

 

本文博客源地址:http://blog.csdn.net/ypist

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值