PopupWindow


1 PopupWindow & Dialog:
  PopupWindow是一个阻塞式的弹出框(在我们退出这个弹出框之前,程序会一直等待),Dialog非阻塞式弹出框(后台还可以做其他事情)
2 PopupWindow使用步骤总结
  Ⅰ  自定义PopupWindow布局文件,并获取获取其实例
  Ⅱ  创建PopupWindow对象,定义相关属性
  Ⅲ  PopupWindow界面显示
  Ⅳ  响应自定义布局的事件


布局文件(主):popupwindow_demo.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" >
 
     <TextView
 android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:gravity="center_horizontal"
         android:paddingBottom="25dp"
         android:paddingTop="15dp"
         android:text="SkySeraph Android学习专题:PopupWindow"
         android:textColor="#FFFF00"
         android:textSize="15dp" 
         android:id="@+id/popupwindow_demo_TV">
     </TextView>
 
     
     <LinearLayout
 android:layout_width="fill_parent"
         android:layout_height="wrap_content"        
         android:orientation="vertical" 
         android:layout_gravity="center">
 
         <Button
 android:id="@+id/popupwindow_demo_btn01"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:text="PopupWindow弹出对话框演示(动画)" >
         </Button>
         
         
     </LinearLayout>
 
 </LinearLayout>

布局文件(自定义):popupwindow_demo01.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:background="#000000"
     android:orientation="vertical" >
 
     <TextView
 android:id="@+id/popupwindow_demo01_TVUsername"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_marginLeft="20dip"
         android:layout_marginRight="20dip"
         android:text="用户名"
         android:textAppearance="?android:attr/textAppearanceMedium" />
 
     <EditText
 android:id="@+id/popupwindow_demo01_ETUername"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_marginLeft="20dip"
         android:layout_marginRight="20dip"
         android:capitalize="none"
         android:textAppearance="?android:attr/textAppearanceMedium" />
 
     <TextView
 android:id="@+id/popupwindow_demo01_TVPassword"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_marginLeft="20dip"
         android:layout_marginRight="20dip"
         android:text="密码"
         android:textAppearance="?android:attr/textAppearanceMedium" />
 
     <EditText
 android:id="@+id/popupwindow_demo01_ETPassword"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_marginLeft="20dip"
         android:layout_marginRight="20dip"
         android:capitalize="none"
         android:password="true"
         android:textAppearance="?android:attr/textAppearanceMedium" 
         android:inputType="numberSigned"/>
 
     <LinearLayout
 android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:gravity="center" >
 
         <Button
 android:id="@+id/popupwindow_demo01_BtnOK"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="60"
             android:text="确定" >
         </Button>
 
         <Button
 android:id="@+id/popupwindow_demo01_BtnCancel"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_weight="60"
             android:text="取消" >
         </Button>
     </LinearLayout>
 
 </LinearLayout>


 java代码:popupwindow_demo.java

public class popupwindow_demo extends Activity
 {    
     // //
     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
         // TODO Auto-generated method stub
         super.onCreate(savedInstanceState);
         setContentView(R.layout.popupwindow_demo);
         findViews();
     }
 
     // //
     private void findViews()
     {
         Button btn1 = (Button) findViewById(R.id.popupwindow_demo_btn01);
         btn1.setOnClickListener(new OnClickListener()
         {
             public void onClick(View v)
             {
                 // TODO Auto-generated method stub
                 showPopupWindow1(popupwindow_demo.this, 
                         popupwindow_demo.this.findViewById(R.id.popupwindow_demo_btn01)); 
                 return;
             }            
         });        
     }
     // //    
     
     
 // //
     private void showPopupWindow1(Context context, View parent)
     {        
         ///
 // 【Ⅰ】 获取自定义popupWindow布局文件
 //方式一:
 //LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);        
 //final View vPopupWindow = inflater.inflate(R.layout.popupwindow_demo01, null, false);
 //方式二:
         final View vPopupWindow = getLayoutInflater().inflate(R.layout.popupwindow_demo01,
                 null,false);                               // 加载popupWindow的布局文件     
         vPopupWindow.setBackgroundColor(Color.GREEN);      // 设置popupWindow的背景颜色   
         
         ///    
 // 【Ⅱ】 创建PopupWindow实例    
         final PopupWindow pw = new PopupWindow(vPopupWindow, 300, 300, true);// 声明一个弹出框 ,最后一个参数和setFocusable对应
         pw.setContentView(vPopupWindow);   // 为弹出框设定自定义的布局 
 //pw.setBackgroundDrawable(getResources().getDrawable(R.drawable.rounded_corners_pop));//设置整个popupwindow的样式。
         pw.setAnimationStyle(R.style.AnimationPreview); // 设置动画效果
         pw.setFocusable(true); //默认为false,如果不设置为true,PopupWindow里面是获取不到焦点的,那么如果PopupWindow里面有输入框等的话就无法输入。
         
 // /
 // 【Ⅲ】 显示popupWindow对话框
 // 获取屏幕和对话框各自高宽
         int screenWidth, screenHeight, dialgoWidth, dialgoheight;
         screenWidth = popupwindow_demo.this.getWindowManager().getDefaultDisplay().getWidth();
         screenHeight = popupwindow_demo.this.getWindowManager().getDefaultDisplay().getHeight();
         dialgoWidth = pw.getWidth();
         dialgoheight = pw.getHeight();
         // pw.showAsDropDown(parent); //以自己为Anchor,不偏移
 // pw.showAsDropDown(parent, (screenWidth-dialgoWidth)/2, 0);//以自己为Anchor,偏移(screenWidth-dialgoWidth)/2, 0)--按钮正下方
         pw.showAtLocation(parent, Gravity.CENTER, 0, 0);// 以屏幕中心为参照,不偏移
 // pw.showAtLocation(parent, Gravity.BOTTOM, 0, 0);//以屏幕左下角为参照
         /* 注释:
          * 【showAsDropDown & showAtLocation】
          * showAsDropDown(View anchor)相对某个控件的位置(正左下方),无偏移 
          * showAsDropDown(View anchor, int xoff, intyoff) 相对某个控件的位置,有偏移(正数表示下方右边,负数表示(上方左边)) 
          * showAtLocation(View parent,int gravity, int x, int y) gravity依靠父布局的位置如Gravity.CENTER  x y 坐标值
 */
 
         ///
 // 【Ⅳ】自定义布局中的事件响应
 // OK按钮及其处理事件
         Button btnOK = (Button) vPopupWindow.findViewById(R.id.popupwindow_demo01_BtnOK);
         btnOK.setOnClickListener(new OnClickListener()
         {
             @Override
             public void onClick(View v)
             {
                 TextView textView = (TextView)findViewById(R.id.popupwindow_demo_TV);
                 EditText edtUsername = (EditText) vPopupWindow.findViewById(R.id.popupwindow_demo01_ETUername);
                 edtUsername.setHint("请输入您的用户名!");
                 edtUsername.setInputType(InputType.TYPE_TEXT_FLAG_AUTO_CORRECT); 
                 edtUsername.setInputType(InputType.TYPE_NULL); //不显示软键盘
                 EditText edtPassword = (EditText) vPopupWindow.findViewById(R.id.popupwindow_demo01_ETPassword);
                 edtPassword.setHint("请输入您的密码!");
                 textView.setText("你输入的用户名是:" + edtUsername.getText().toString() + "\n" 
                                 +"你输入的密码是:"+edtPassword.getText().toString());
                 pw.dismiss();// 关闭
             }
         }); 
         // Cancel按钮及其处理事件
         Button btnCancel = (Button) vPopupWindow.findViewById(R.id.popupwindow_demo01_BtnCancel);
         btnCancel.setOnClickListener(new OnClickListener()
         {
             @Override
             public void onClick(View v)
             {
                 pw.dismiss();// 关闭
             }
         }); 
     }
 }


动画效果:

<?xml version="1.0" encoding="utf-8"?>
 <resources xmlns:android="http://schemas.android.com/apk/res/android">
     <!-- popupwindow_demo 中动画文件 -->
     <style name="AnimationPreview">
         <item name="android:windowEnterAnimation">@anim/fade_in</item>
         <item name="android:windowExitAnimation">@anim/fade_out</item>
     </style>
 
 </resources>

fade_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android" >
 
     <scale
 android:duration="700"
         android:fillAfter="false"
         android:fromXScale="0.0"
         android:fromYScale="0.0"
         android:interpolator="@android:anim/accelerate_decelerate_interpolator"
         android:pivotX="50%"
         android:pivotY="50%"
         android:toXScale="1.0"
         android:toYScale="1.0" />
 
 </set>


fade_out.xml


<set xmlns:android="http://schemas.android.com/apk/res/android" >
 
     <scale
 android:duration="700"
         android:fillAfter="false"
         android:fromXScale="1.0"
         android:fromYScale="1.0"
         android:interpolator="@android:anim/accelerate_decelerate_interpolator"
         android:pivotX="50%"
         android:pivotY="50%"
         android:toXScale="0.0"
         android:toYScale="0.0" />
 
 </set>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值