自定义评论Dialog

来源:互联网 发布:后台调用前台js方法 编辑:程序博客网 时间:2024/05/22 14:41

前面的一篇博客中简单介绍了自定义Dialog,并通过AlertDialog实现了基本的功能。这篇博客中通过继承Dialog的方式实现发表评论的Dialog。


  1. 自定义CommentDialog继承自Dialog,实现评论对话框的布局显示和基本监听事件

评论模块中需要用到EditText,要想实现显示对话框EditText自动获取焦点弹出软键盘需要为Dialog依赖的windown设置下面属性:
WindowManager.LayoutParams attributes = window.getAttributes();
attributes.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;//显示dialog的时候,就显示软键盘
attributes.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;//弹窗获取焦点

 public CommentDialog(Context context, int themeResId) {        super(context, themeResId);        setContentView(R.layout.comment_dialog_layout);        setCanceledOnTouchOutside(true);        Window window = getWindow();        window.setGravity(Gravity.BOTTOM);//设置dialog的显示位置        WindowManager.LayoutParams attributes = window.getAttributes();        attributes.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;//显示dialog的时候,就显示软键盘        attributes.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;//弹窗获取焦点        WindowManager windowManager = window.getWindowManager();        Display display = windowManager.getDefaultDisplay();        attributes.width = (int) (display.getWidth());//设置dialog的宽度占满整个屏幕        window.setAttributes(attributes);        initView();    }

2.设置Dialog的样式

  <!--评论dialog的样式-->    <style name="comment_style" parent="Theme.AppCompat.Dialog">        <item name="android:windowFrame">@null</item>//Dialog的windowFrame框为无        <item name="android:windowNoTitle">true</item>//是否显示title        <item name="android:windowIsFloating">true</item>//是否浮现在activity之上        <item name="android:windowBackground">@android:color/transparent</item>//设置dialog的背景        <item name="android:windowContentOverlay">@null</item>//设置窗体内容背景    </style>

3.在代码中使用CommentDialog

  commentView.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                commentDialog = new CommentDialog(MainActivity.this).setListener(new CommentDialog.Listener() {                    @Override                    public void commentContent(String content) {//                        TODO 发表评论 发表成功后取消Dialog                        handler.sendEmptyMessageDelayed(1, 2000);                    }                });                commentDialog.show();            }        });

4.效果图

这里写图片描述

5.源码地址
https://github.com/violinlin/CommentDialog.git

0 0
原创粉丝点击