前面的一篇博客中简单介绍了自定义Dialog,并通过AlertDialog实现了基本的功能。这篇博客中通过继承Dialog的方式实现发表评论的Dialog。
- 自定义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);