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

一、基础知识:

 

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

2. PopupWindow完全依赖Layout做外观

 

 

 

二、使用方法:

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

<?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="#808080" 
    >  
    <ImageButton 
       	android:id="@+id/username_image"
        android:layout_x="@string/username_image_x"
        android:layout_y="@string/username_image_y"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" />
    <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>  


 

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

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

 

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

public void showPopupWindow(Context context,View parent){
    	/*** ==== Start 创建popupWindow对话框 ==== ***/   
        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);  
        /*** ==== End 创建popupWindow对话框 ==== ***/
        
        /*** ==== Start 设置popupWindow对话框 上面的按钮显示及响应  ==== ***/  
        ImageButton loginImageButton = (ImageButton)vPopupWindow.findViewById(R.id.username_image);
        loginImageButton.setBackgroundResource(R.drawable.login);
        
        //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();//关闭   
            }  
        });  
        /*** ==== End 设置popupWindow对话框 上面的按钮显示及响应  ==== ***/
        
        /*** ==== Start 显示popupWindow对话框 ==== ***/   
        //设置SelectPicPopupWindow弹出窗体动画效果   
        pw.setAnimationStyle(R.style.FromRightAnimation);	// 从屏幕右边移进来
        //pw.setAnimationStyle(R.style.PopupAnimation);		// 从小放大
        // 指定PopupWindow的显示位置
        //pw.showAtLocation(vPopupWindow,0,20,100);
        // 显示在屏幕的最中央
        pw.showAtLocation(parent, Gravity.CENTER, 0, 0);
        pw.update();
        /*** ==== End 显示popupWindow对话框 ==== ***/
    }  

 


 

三、效果展示:

 

 

 

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

 

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

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值