码迷,mamicode.com
首页 > 其他好文 > 详细

自定义dialog样式,自动弹出软件盘

时间:2015-11-25 18:49:43      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:

开发中android自带dialog样式有时候不能满足我们的需求,这时候就需要自定义样式了,调用dialog的setView方法可以自定义布局,代码如下

技术分享
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

        View alertDialogView = View.inflate(context, R.layout.comment_input_layout, null);
        final AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.setView(alertDialogView, 0, 0, 0, 0);

        final EditText editText = (EditText) alertDialogView.findViewById(R.id.etContent);

        alertDialog.show();

//      设置dialog从底部出现动画
        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.comment_input_layout);
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.comment_input_dilog_anim_style);
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = alertDialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth()); //设置宽度
        window.setAttributes(lp);
        });
View Code

通过window设置从底部进入动画,必须在dialog.show()后面设置

但是这样写完之后,如果布局中有edittext,当dialog显示的时候,并不会自动弹出软件盘,于是需要在show()方法之前加上如下代码:

InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(etContent, InputMethodManager.RESULT_SHOWN);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);

toggleSoftInput方法是切换软键盘的显示与隐藏,第一个参数不能传InputMethodManager.SHOW_FORCED,否则关闭dialog的时候不能关掉软键盘。

本来以为这样已经完美了。。。但是问题又来了,在声明edittext后声明了取消和确定两个button,并设置了监听,但是监听不起作用,按钮点击没有任何反应。解决办法:

在设置动画,宽度等操作后面再用window.findViewById声明控件,设置监听事件。具体原因不知道,感觉是window.setContentView(R.layout.comment_input_layout);

把原来的控件覆盖掉了,最后附上完整代码:

技术分享
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);

        View alertDialogView = View.inflate(context, R.layout.comment_input_layout, null);
        final AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.setView(alertDialogView, 0, 0, 0, 0);

        final EditText editText = (EditText) alertDialogView.findViewById(R.id.etContent);

        //自动弹出软键盘
        alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            public void onShow(DialogInterface dialog) {
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(editText, InputMethodManager.RESULT_SHOWN);
                imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
            }
        });
        alertDialog.show();

        Window window = alertDialog.getWindow();
        window.setContentView(R.layout.comment_input_layout);
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.comment_input_dilog_anim_style);
        WindowManager windowManager = ((Activity) context).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        WindowManager.LayoutParams lp = alertDialog.getWindow().getAttributes();
        lp.width = (int) (display.getWidth()); //设置宽度
        window.setAttributes(lp);

        ImageButton ibtnClose=(ImageButton)window.findViewById(R.id.ibtnClose);
        final ImageButton ibtnRight=(ImageButton)window.findViewById(R.id.ibtnRight);
        final EditText etContent = (EditText) window.findViewById(R.id.etContent);
        ibtnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                alertDialog.cancel();
            }
        });

        ibtnRight.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(ibtnRightClickListener!=null){
                    ibtnRightClickListener.sendComment(etContent.getText().toString());
                }
                alertDialog.cancel();
            }
        });
View Code

 

自定义dialog样式,自动弹出软件盘

标签:

原文地址:http://www.cnblogs.com/3A87/p/4995240.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!