Android之PopupWindow弹出对话框

Android之PopupWindow弹出对话框

Android的对话框常用的有两种:PopupWindow和AlertDialog。

  1. popupWindow是一个阻塞式的弹出框,这就意味着在我们退出这个弹出框之前,程序会一直等待,,这就意味着在我们退出这个弹出框之前,程序会一直等待, 
  2.      *这和AlertDialog不同哦,AlertDialog是非阻塞式弹出框,AlertDialog弹出的时候,后台可是还可以做其他事情的哦。 

 

PopupWindow顾名思义为弹出菜单,不同于AlertDialog对话框,PopupWindow弹出的位置可以很多变化,按照有无偏移分,可以分为无偏移和偏移两种;按照参照类型不同又可以分为两种:相对某个控件(Anchor锚)的位置和父容器内部的相对位置。具体如下:

函数简介
showAsDropDown(View anchor)相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff)相对某个控件的位置,有偏移(正数表示下方右边,负数表示(上方左边))
showAtLocation(View parent, int gravity, int x, int y)父容器容器相对位置,例如正中央Gravity.CENTER,下方Gravity.BOTTOM等

 

下面是运行程序截图:

 

 

程序代码:

布局:main.xml

main.xml
<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
    Android:id="@+id/layout"  
    Android:orientation="vertical"  
    Android:layout_width="fill_parent"  
    Android:layout_height="fill_parent"  
    >  
<TextView    
    Android:id="@+id/tv_showText"  
    Android:layout_width="fill_parent"    
    Android:layout_height="wrap_content"    
    Android:gravity="center"  
    Android:text="@string/hello"  
    Android:textSize="22px"  
    />  
       
<Button  
    Android:id="@+id/bt_PopupWindow1"  
    Android:text="以自己为Anchor,不偏移"  
    Android:layout_width="fill_parent"    
    Android:layout_height="wrap_content"  
    />     
  
<Button  
    Android:id="@+id/bt_PopupWindow2"  
    Android:text="以自己为Anchor,正下方"  
    Android:layout_width="fill_parent"    
    Android:layout_height="wrap_content"  
    />     
       
<Button  
    Android:id="@+id/bt_PopupWindow3"  
    Android:text="以屏幕中心为参照,不偏移(正中间)"  
    Android:layout_width="fill_parent"    
    Android:layout_height="wrap_content"  
    />     
  
<Button  
    Android:id="@+id/bt_PopupWindow4"  
    Android:text="以屏幕下方为参照,下方中间"  
    Android:layout_width="fill_parent"    
    Android:layout_height="wrap_content"  
    />     
       
     
</LinearLayout>  

自定义对话框dialog.xml    

dialog.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"  
    >  
<TextView    
    Android:id="@+id/tv_tip"  
    Android:layout_width="fill_parent"    
    Android:layout_height="wrap_content"    
    Android:text="请输入内容:"  
    />  
     
 <EditText  
    Android:id="@+id/et_text"  
    Android:layout_width="fill_parent"    
    Android:layout_height="wrap_content"    
 ></EditText>    
    
<LinearLayout  
    Android:gravity="center_horizontal"    
    Android:layout_width="fill_parent"  
    Android:layout_height="fill_parent"  
>     
<Button  
    Android:id="@+id/bt_ok"  
    Android:text="确定"  
    Android:layout_width="100px"    
    Android:layout_height="50px"  
    />     
  
<Button  
    Android:id="@+id/bt_cancle"  
    Android:text="取消"  
    Android:layout_width="100px"    
    Android:layout_height="50px"  
    />     
</LinearLayout>  
</LinearLayout>  
?
import Android.app.Activity;  
import Android.content.Context;  
import Android.content.SharedPreferences.Editor;  
import Android.os.Bundle;  
import Android.util.Log;  
import Android.view.Gravity;  
import Android.view.LayoutInflater;  
import Android.view.View;  
import Android.view.View.OnClickListener;  
import Android.view.ViewGroup.LayoutParams;  
import Android.widget.Button;  
import Android.widget.EditText;  
import Android.widget.Gallery;  
import Android.widget.PopupWindow;  
import Android.widget.TextView;  
   
public class PopupWindowTest extends Activity { //PopupWindow属于不阻塞的对话框,AlertDialog则是阻塞的。  
     private Button bt_popupWindow1;  
     private Button bt_popupWindow2;  
     private Button bt_popupWindow3;  
     private Button bt_popupWindow4;  
     private TextView tv_showText;  
     private PopupWindow popupWindow;  
     private int screenWidth;  
     private int screenHeight;  
     private int dialgoWidth;  
     private int dialgoheight;  
        
   
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) {  
         super .onCreate(savedInstanceState);  
         setContentView(R.layout.main);  
            
         initView();  
     }  
        
     /** 
      * 初始化控件和响应事件 
      */ 
     private void initView() {  
         bt_popupWindow1 = (Button)findViewById(R.id.bt_PopupWindow1);  
         bt_popupWindow2 = (Button)findViewById(R.id.bt_PopupWindow2);  
         bt_popupWindow3 = (Button)findViewById(R.id.bt_PopupWindow3);  
         bt_popupWindow4 = (Button)findViewById(R.id.bt_PopupWindow4);  
         tv_showText = (TextView)findViewById(R.id.tv_showText);  
            
         bt_popupWindow1.setOnClickListener( new ClickEvent());  
         bt_popupWindow2.setOnClickListener( new ClickEvent());  
         bt_popupWindow3.setOnClickListener( new ClickEvent());  
         bt_popupWindow4.setOnClickListener( new ClickEvent());  
            
            
            
     }  
        
     /** 
      * 按钮点击事件处理 
      * @author Kobi 
     
      */ 
     private class ClickEvent implements OnClickListener {  
            
         @Override 
         public void onClick(View v) {  
             // TODO Auto-generated method stub  
             switch (v.getId()) {  
                    
             case R.id.bt_PopupWindow1:  //以自己为Anchor,不偏移  
                 getPopupWindow();  
                 popupWindow.showAsDropDown(v);  
                 break ;  
                    
             case R.id.bt_PopupWindow2:  //以自己为Anchor,偏移(screenWidth-dialgoWidth)/2, 0)--按钮正下方  
                 getPopupWindow();  
                 popupWindow.showAsDropDown(v, (screenWidth-dialgoWidth)/ 2 , 0 );  
                 break ;  
                    
             case R.id.bt_PopupWindow3:  //以屏幕中心为参照,不偏移  
                 getPopupWindow();  
                 popupWindow.showAtLocation(findViewById(R.id.layout), Gravity.CENTER, 0 , 0 );  
                 break ;  
                    
             case R.id.bt_PopupWindow4:  //以屏幕左下角为参照,偏移(screenWidth-dialgoWidth)/2, 0) --屏幕下方中央  
                 getPopupWindow();  
                 popupWindow.showAtLocation(findViewById(R.id.layout),   
                         Gravity.BOTTOM, 0 , 0 );  
                 break ;  
                    
             default :  
                 break ;  
             }  
         }  
            
     }  
   
     /** 
      * 创建PopupWindow 
      */ 
     protected void initPopuptWindow() {  
         // TODO Auto-generated method stub  
            
            
         View popupWindow_view = getLayoutInflater().inflate(    //获取自定义布局文件dialog.xml的视图  
                 R.layout.dialog, null , false );  
            
         popupWindow = new PopupWindow(popupWindow_view, 200 , 150 , true ); //创建PopupWindow实例  
            
         Button bt_ok = (Button)popupWindow_view.findViewById(R.id.bt_ok);   //dialog.xml视图里面的控件  
         Button bt_cancle = (Button)popupWindow_view.findViewById(R.id.bt_cancle);  
         final EditText et_text = (EditText)popupWindow_view.findViewById(R.id.et_text);   
            
         bt_ok.setOnClickListener( new OnClickListener() {  
                
             @Override 
             public void onClick(View v) {  
                 // TODO Auto-generated method stub  
                 tv_showText.setText(et_text.getText()); //在标签里显示内容  
                 popupWindow.dismiss();                  //对话框消失  
             }  
         });  
            
         bt_cancle.setOnClickListener( new OnClickListener() {  
                
             @Override 
             public void onClick(View v) {  
                 // TODO Auto-generated method stub  
                 popupWindow.dismiss();  
             }  
         });  
            
         //获取屏幕和对话框各自高宽  
         screenWidth = PopupWindowTest. this .getWindowManager().getDefaultDisplay().getWidth();  
         screenHeight = PopupWindowTest. this .getWindowManager().getDefaultDisplay().getHeight();  
   
         dialgoWidth = popupWindow.getWidth();  
         dialgoheight = popupWindow.getHeight();  
            
     }  
        
     /* 
      * 获取PopupWindow实例 
      */ 
     private void getPopupWindow() {  
            
         if ( null != popupWindow) {  
             popupWindow.dismiss();  
             return ;  
         } else {  
             initPopuptWindow();  
         }  
     }  
   
     @Override 
     protected void onPause() {  
         // TODO Auto-generated method stub  
         super .onPause();  
         Log.e( "ActivityState" , "onPause" );  
     }  
   
     @Override 
     protected void onResume() {  
         // TODO Auto-generated method stub  
         super .onResume();  
         Log.e( "ActivityState" , "onResume" );  
     }  
        
        
   
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Android Studio中的PopupWindow是一个弹出式窗口,可以在屏幕上方或下方显示。它可以用来显示一些额外的信息或者提供一些额外的操作。PopupWindow可以自定义布局和样式,可以通过代码控制它的位置和大小。在Android应用程序中,PopupWindow通常用于显示菜单、提示信息、弹出对话框等。 ### 回答2: Android Studio PopupWindow是一个常用的组件,可以用来显示类似下拉菜单、弹出窗口、悬浮窗口等UI控件。该组件可以显示在Activity的任何位置,因此非常灵活。下面,我将从PopupWindow的使用、事件响应和动画效果三个方面阐述此组件。 使用PopupWindow组件 使用PopupWindow组件步骤如下: 1. 实例化PopupWindow对象,设置宽度、高度和布局文件; 2. 设置PopupWindow的属性:是否可获得焦点、是否点击非PopupWindow区域自动关闭等; 3. 在需要弹出PopupWindow时,用showAsDropDown或showAtLocation方法设置弹出位置; 4. 在需要关闭PopupWindow时,用dismiss方法关闭。 具体实现如下: // 实例化PopupWindow PopupWindow popupWindow = new PopupWindow(view, width, height); // 设置PopupWindow属性 popupWindow.setFocusable(true); popupWindow.setOutsideTouchable(true); // 弹出PopupWindow popupWindow.showAsDropDown(anchorView, xoff, yoff); // 关闭PopupWindow popupWindow.dismiss(); PopupWindow组件设置事件响应 PopupWindow组件有以下两种事件响应: 1. PopupWindow外部可点击区域的事件响应:通常是为了在关闭PopupWindow时,点击PopupWindow外部区域触发关闭方法。实现如下: popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { backgroundAlpha(1.0f);//还原背景色 } }); 2. PopupWindow内部控件的事件响应:通常是为了在PopupWindow弹出后,控制内部控件的事件响应。实现如下: view.findViewById(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO: 点击按钮后的处理逻辑 } }); PopupWindow组件设置动画效果 PopupWindow组件可以设置进出场动画,使得弹出窗口更加美观。实现如下: // 动画文件:popup_enter.xml和popup_exit.xml // 在res/anim目录下新建两个XML文件 // popup_enter.xml实现PopupWindow从下方弹出的动画效果 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="100%p" android:toYDelta="0" android:duration="300" /> <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /> </set> // popup_exit.xml实现PopupWindow向下消失的动画效果 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:fromYDelta="0" android:toYDelta="100%p" android:duration="300" /> <alpha android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="300" /> </set> // 设置PopupWindow进出场动画效果 popupWindow.setAnimationStyle(R.anim.popup_enter); // 进入动画 popupWindow.setAnimationStyle(R.anim.popup_exit); // 退出动画 总之,PopupWindow组件是Android Studio中一种非常实用的弹出式UI组件,具有很高的可定制性,通过事件响应和动画效果的设置,可以实现各种各样的应用场景。 ### 回答3: Android Studio中PopupWindow是一个弹出式窗口,它可以在用户当前界面弹出一个新的视图作为临时层级。通常这个视图被用来显示用户的一些选择或操作结果,它可以从屏幕的任何位置并且大小和内容也可以自定义。PopupWindow可以在一个View下方弹出,它遮盖了其他View但并不完全覆盖,因此用户仍可以看到其他的UI元素。弹出窗口有多种用途,包括弹出一个对话框、菜单、列表、日期选择器、时间选择器等等。下面是一些关于如何在Android Studio中使用PopupWindow的例子: 1. 创建一个弹出窗口 要创建一个弹出窗口,首先需要创建一个包含一个按钮的布局文件,然后在MainActivity.java中添加以下代码: ``` public void showPopup(View v) { PopupWindow popupWindow = new PopupWindow(this); View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null); popupWindow.setContentView(popupView); popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); popupWindow.showAsDropDown(v); } ``` 2. 弹出一个菜单 要弹出一个菜单,可以使用PopupMenu类。首先在res/menu文件夹中创建一个menu.xml文件,然后在MainActivity.java中添加以下代码: ``` public void showMenu(View v) { PopupMenu popupMenu = new PopupMenu(this, v); MenuInflater inflater = popupMenu.getMenuInflater(); inflater.inflate(R.menu.menu, popupMenu.getMenu()); popupMenu.show(); } ``` 3. 创建一个自定义布局的弹出窗口 要创建一个自定义布局的弹出窗口,可以在layout文件夹中创建一个自己的布局文件,然后在MainActivity.java中添加以下代码: ``` public void showCustomPopup(View v) { PopupWindow popupWindow = new PopupWindow(this); View popupView = getLayoutInflater().inflate(R.layout.custom_layout, null); popupWindow.setContentView(popupView); popupWindow.setWidth(WindowManager.LayoutParams.MATCH_PARENT); popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); popupWindow.showAtLocation(v, Gravity.CENTER, 0, 0); Button closeButton = popupView.findViewById(R.id.close_button); closeButton.setOnClickListener(view -> popupWindow.dismiss()); } ``` 总之,PopupWindow是一个非常有用的控件,可以帮助我们在应用程序中构建各种各样的自定义UI效果。虽然它可能需要花费一些时间来构建和调试,但一旦学会使用,就可以为你的应用程序增加无限的功能和灵活性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值