自定义弹窗对话框PopUpWindow

先写popupwindow.xml的布局,,一个输入框和一个按钮

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="#00ff00" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:orientation="vertical" >  
  11.   
  12.         <EditText  
  13.             android:id="@+id/edit_01"  
  14.             android:layout_width="wrap_content"  
  15.             android:layout_height="wrap_content"  
  16.             android:hint="请输入用户名" />  
  17.   
  18.         <Button  
  19.             android:layout_gravity="center_horizontal"  
  20.             android:id="@+id/pop_btn"  
  21.             android:layout_width="wrap_content"  
  22.             android:layout_height="wrap_content"  
  23.             android:text="登录" />  
  24.     </LinearLayout>  
  25.   
  26. </RelativeLayout>  

activity_main.xml

[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <Button  
  7.        android:id="@+id/btn"  
  8.         android:onClick="xianShiPop"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_centerInParent="true"  
  12.         android:text="登录" />  
  13.   
  14. </RelativeLayout>  

MainActivity中引入popupwindow的布局 设置popupwindow显示的位置

[html]  view plain  copy
  1. public class MainActivity extends Activity {  
  2.   
  3.     private View contentView;  
  4.     private PopupWindow popupWindow;  
  5.     private Button button;  
  6.     private View parent;  
  7.     private Button pop_btn;  
  8.     private EditText edit_01;  
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.           
  14.         button = (Button) findViewById(R.id.btn);  
  15.           
  16.         /**  
  17.          * popupWindown可以展示一个视图,,,是一个容器,并且悬浮在当前的activity上面  
  18.          *   
  19.          * contentView, 窗体要显示的视图...需要自己写在布局里面,,,,要把布局转换成view,,,inflate打气筒  
  20.          * width, 窗体弹出的宽度  LayoutParams.WRAP_CONTENT....import android.view.ViewGroup.LayoutParams;  
  21.          * height窗体弹出的高度  
  22.          *   
  23.          * 注意...popupWindown的宽度和高度是由代码中设置的宽度和高度来控制的,,,跟布局中没有关系  
  24.          */  
  25.         contentView = View.inflate(MainActivity.this, R.layout.pop_layout, null);  
  26.         //父窗体  
  27.         parent = View.inflate(MainActivity.this, R.layout.activity_main, null);  
  28.         //通过构造方法创建一个popupWindown  
  29.         popupWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  30.           
  31.         /**  
  32.          * 出现的问题,,,点击周围不消失,,点击返回键直接退出这个activity...里面的editText控件不能输入  
  33.          */  
  34.         popupWindow.setTouchable(true);//设置窗体可以触摸,,,默认就是true  
  35.         popupWindow.setFocusable(true);//让窗体获取到焦点...一般情况下窗体里面的控件都能获取到焦点,但是editText特殊  
  36.           
  37.         popupWindow.setOutsideTouchable(true);//设置窗体外部可以触摸  
  38.         popupWindow.setBackgroundDrawable(new BitmapDrawable());//设置背景  
  39.           
  40.         //popupWindown里面的控件怎么去处理?  
  41.         pop_btn = (Button) contentView.findViewById(R.id.pop_btn);  
  42.         edit_01 = (EditText) contentView.findViewById(R.id.edit_01);  
  43.           
  44.         pop_btn.setOnClickListener(new OnClickListener() {  
  45.               
  46.             @Override  
  47.             public void onClick(View v) {  
  48.                 Toast.makeText(MainActivity.this, edit_01.getText().toString(), 0).show();  
  49.                 //弹出窗体消失  
  50.                 popupWindow.dismiss();  
  51.                   
  52.             }  
  53.         });  
  54.     }  
  55.     //按钮的点击事件  
  56.     public void xianShiPop(View view) {  
  57.         //显示一个窗体...只做显示的功能  
  58.         //popupWindow.showAsDropDown(button);//展示在控件的左下方  
  59.         //popupWindow.showAsDropDown(button, -100, 40);  
  60.         /**  
  61.          * 在父窗体的某个位置展示  
  62.          * View parent,   
  63.          * int gravity, 重力/方向  Gravity.BOTTOM...Gravity.CENTER  
  64.          * int x,   
  65.          * int y  
  66.          */  
  67.         popupWindow.showAtLocation(parent, Gravity.CENTER, 0, 0);  
  68.     }  
  69.   
  70. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值