AlertDialog无法弹出软键盘问题、软键盘显示与隐藏

AlertDialog好像本身有限制,可以改用Dialog。

Dialog的以下设置:

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去除title
        super.onCreate(savedInstanceState);

        int width =  AppContext.getInstance().getWindowWidth()*5/6;
       // int height = view.getMeasuredHeight();

        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER); //可设置dialog的位置
        window.getDecorView().setPadding(20, 0, 20, 0); //消除边距


        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = width;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

        window.setAttributes(lp);

        View view = View.inflate(getContext(),R.layout.view_pswdialog,null);


        setContentView(view);

        pswText = findViewById(R.id.pswdialog_pswtext);
        CloseClick = findViewById(R.id.pswdialog_closeclick);
        CloseClick.setOnClickListener(CloseClicklistener);
        setCancelable(true);

        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {

            }
        });

软键盘显示与隐藏:

   private void ShowSoftInput(){
        pswText.setFocusable(true);
        pswText.setFocusableInTouchMode(true);
        pswText.requestFocus();
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        // 显示软键盘
        imm.showSoftInput(pswText, 0);
        pswText.ShowKeyBord();

    }

    private void HideSoftInput(){
        if(Build.VERSION.SDK_INT >= 17){
            Loger.e("getSoftButtonsBarHeight",getSoftButtonsBarHeight()+"");
            if(!isSoftShowing()){
                return;
            }
        }
        // 隐藏软键盘
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.HIDE_NOT_ALWAYS, 0);
        pswText.hideKeyBord();

    }

    @TargetApi(17)
    private int getSoftButtonsBarHeight() {
        DisplayMetrics metrics = new DisplayMetrics();
        //这个方法获取可能不是真实屏幕的高度
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        int usableHeight = metrics.heightPixels;
        //获取当前屏幕的真实高度
        mActivity.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
        int realHeight = metrics.heightPixels;
        if (realHeight > usableHeight) {
            return realHeight - usableHeight;
        } else {
            return 0;
        }
    }

    private boolean isSoftShowing() {
        //获取当前屏幕内容的高度
        int screenHeight = mActivity.getWindow().getDecorView().getHeight();
        //获取View可见区域的bottom
        Rect rect = new Rect();
        mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);

         return screenHeight - rect.bottom - getSoftButtonsBarHeight() != 0;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值