Dialog对话框样式的四种实现方式

来源:互联网 发布:小榕哥sql注入工具 编辑:程序博客网 时间:2024/06/05 15:29

1.最好的实现方式,重写dialog样式。

   特点:1)需要自定义整个布局内容。

2)可以设置点击对话框外禁止取消窗口。

3)可以弹出输入法。

                4)点返回键可以取消。

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.ebt.app.msettings.view;  
  2.   
  3. import android.app.Dialog;  
  4. import android.content.Context;  
  5. import android.os.Bundle;  
  6. import android.view.Gravity;  
  7. import android.view.LayoutInflater;  
  8. import android.view.ViewGroup;  
  9. import android.view.WindowManager.LayoutParams;  
  10.   
  11. import com.ebt.app.R;  
  12.   
  13. /** 
  14.  * 对话框基类 
  15.  *  
  16.  * @author Allen.li 
  17.  *  
  18.  */  
  19. public class BaseDialog extends Dialog{  
  20.     private Context context;  
  21.     private LayoutInflater inflater;  
  22.     private ViewGroup view;  
  23.     public BaseDialog(Context context) {  
  24. //      super(context);  
  25.         super(context, R.style.dialog);  
  26.         this.context = context;  
  27.     }  
  28.       
  29.     @Override  
  30.     protected void onCreate(Bundle savedInstanceState) {  
  31.         super.onCreate(savedInstanceState);  
  32.         setCanceledOnTouchOutside(false);//点击对话框外禁止取消窗口  
  33.         LayoutParams params = getWindow().getAttributes();  
  34.         params.windowAnimations = R.style.popupAnimation;  
  35.         getWindow().setGravity(Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);  
  36.         getWindow().setAttributes(  
  37.                 (android.view.WindowManager.LayoutParams) params);  
  38.           
  39.         this.inflater = getLayoutInflater();  
  40.         view = (ViewGroup) inflater.inflate(R.layout.widget_window_msg_phone, null);  
  41.         setContentView(view);  
  42.         view.setFocusable(true);  
  43.         view.setFocusableInTouchMode(true);  
  44.         view.requestFocus();  
  45.     }  
  46. }  


<?xml version="1.0" encoding="utf-8"?>

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <resources>  
  2.   
  3.     <style name="dialog" >  
  4.         <item name="android:windowIsFloating">true</item>  
  5.         <item name="android:windowIsTranslucent">true</item>  
  6.         <item name="android:windowFrame">@null</item>  
  7.         <item name="android:windowNoTitle">true</item>  
  8.         <item name="android:colorBackgroundCacheHint">@null</item>  
  9.         <item name="android:windowBackground">@color/full_transparent</item>  
  10.         <item name="android:windowContentOverlay">@null</item><!-- 这两个属性不加上顶部会出现阴影 -->  
  11.         <item name="android:windowActionModeOverlay">true</item>  
  12.         <!-- <item name="android:backgroundDimEnabled">false</item> -->  
  13.     </style>  
  14.   
  15.   
  16. </resources>  


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. BaseDialog bd = new BaseDialog(getContext());bd.show();  

2.在用AlertDialog来实现。代码同dialog一样,不过输入法不弹出了。解决这个问题参看如下链接,可以弹出输入法,可以设置点击对话框外禁止取消窗口。

http://blog.csdn.NET/fastthinking/article/details/38492389


3.在用activity来实现。样式设置成dialog样式。不过用Acitivity来实现简单的对话框有些不必要。


4.用PopupWindow实现,不过在有输入框的对话框时,PopupWindow不能很好的实现点击返回键退出。原因如下:


1).发现PopupWindow一个bug。

只有获取焦点才能弹出输入法,如果PopupWindow的ContentView想弹出输入法的话,必须设置PopupWindow获取焦点,即PopupWindow.setFocusable(true);

但是会导致设置禁止点击弹出窗口外失败,PopupWindow.setOutsideTouchable(false);即点击弹出窗口外会取消窗口。因为

setOutsideTouchable的设置仅仅对处于未获取焦点的且处于可触摸模式的PopupWindow起作用。setOutsideTouchable默认值是false.也就是说如果PopupWindow获取了焦点,此方法不起作用。点击PopupWindow窗口外的动作响应是取消窗口。

原文注释:

Open Declarationvoid android.widget.PopupWindow.setOutsideTouchable(boolean touchable)

Controls whether the pop-up will be informed of touch events outside of its window.This only makes sense for pop-ups that are touchable but not focusable, which means touches outside of the window will be delivered to the window behind. The default is false.

If the popup is showing, calling this method will take effect only the next time the popup is shown or through a manual call to one of theupdate() methods.

Parameters:
touchable true if the popup should receive outside touch events, false otherwise
See Also:
isOutsideTouchable()
isShowing()
update()


2).如果想设置点击PopupWindow外事件不取消PopupWindow的话,必须如下设置,

PopupWindow.setTouchable(true);//默认
PopupWindow.setFocusable(false);//取消获取焦点,这会导致无法弹出输入法
PopupWindow.setOutsideTouchable(false);


3).看PopupWindow源码知,点击PopupWindow外面区域,自动dismiss,需要调用

PopupWindow.setBackgroundDrawable(new ColorDrawable());//这个很关键

PopupWindow.setOutsideTouchable(true);


4).需要的效果之一,点击点击PopupWindow外面区域,不dismiss对话框,并且点击弹出窗口的输入组件会弹出输入法。(设置PopupWindow.setFocusable(true))

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.               View contentView = LayoutInflater.from(getContext()).inflate(  
  2.         R.layout.widget_window_security, null);  
  3. // 重写onKeyListener,获取焦点的视图上捕获返回键,取消弹出窗口  
  4. contentView.setOnKeyListener(new OnKeyListener() {  
  5.     @Override  
  6.     public boolean onKey(View v, int keyCode, KeyEvent event) {  
  7.         if (keyCode == KeyEvent.KEYCODE_BACK) {  
  8.             if (securityWindow != null) {  
  9.                 securityWindow.dismiss();  
  10.             }  
  11.             return true;  
  12.         }  
  13.         return false;  
  14.     }  
  15. });  
  16. Button mBtnCancel = (Button)contentView.findViewById(R.id.window_btn_cancel);  
  17. mBtnCancel.setOnClickListener(this);  
  18. Button mBtnFinish = (Button)contentView.findViewById(R.id.window_btn_finish);  
  19. mBtnFinish.setOnClickListener(this);  
  20. mEditPwd = (EditText) contentView.findViewById(R.id.window_input_pwd);  
  21.               //如果还想实现返回键退出窗口效果,只能是在获取焦点的视图上捕捉返回按钮,或者是Activity捕捉返回按钮。  
  22. mEditPwd .setOnKeyListener(new OnKeyListener() {  
  23.     @Override  
  24.     public boolean onKey(View v, int keyCode, KeyEvent event) {  
  25.         if (keyCode == KeyEvent.KEYCODE_BACK) {  
  26.             if (securityWindow != null) {  
  27.                 securityWindow.dismiss();  
  28.             }  
  29.             return true;  
  30.         }  
  31.         return false;  
  32.     }  
  33. });  
  34. contentView.setFocusable(true); // 这个很重要  
  35. contentView.setFocusableInTouchMode(true);  
  36.   
  37. securityWindow = new PopupWindow(contentView, LayoutParams.WRAP_CONTENT,  
  38.         LayoutParams.WRAP_CONTENT, true);  
  39.   
  40.         securityWindow.setBackgroundDrawable(new ColorDrawable());//这句不能加,加了点击空白处就取消窗口啦  
  41. securityWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);  
  42. securityWindow  
  43.         .setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);  
  44. securityWindow.setFocusable(true);//设置允许获取焦点,可以弹出输入法  
  45.   
  46. securityWindow.setWidth(UIHelper.dip2px(getContext(), 520));  
  47. securityWindow.setHeight(UIHelper.dip2px(getContext(), 250));  
  48. securityWindow.showAtLocation(this, Gravity.CENTER, 00);  

5).看PopupWindow源码知,PopupWindow的PopupViewContainer类是私有的,用来控制显示和处理点击事件,包括点击窗口外取消窗口,没法重写它来处理点击窗口外取消窗口事件。

0 0
原创粉丝点击