美文网首页
关于软键盘的处理,理论上适应各种情况

关于软键盘的处理,理论上适应各种情况

作者: 小慕汐 | 来源:发表于2018-07-23 17:51 被阅读12次

    如果有问题请联系我,但我不一定在

    放关键代码

    public class KeyboardDialog extends BaseAlertDialog implements ViewTreeObserver.OnGlobalLayoutListener {
    
    private Object mObject;
    
    public KeyboardDialog(Context context) {
       this(context, R.style.style_Dialog_Fullscreen);
       getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
       setCanceledOnTouchOutside(false);// 设置不能点击对话框外边取消当前对话框
    }
    
    protected KeyboardDialog(Context context, int themeResId) {
       super(context, themeResId);
       getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
       setCanceledOnTouchOutside(false);// 设置不能点击对话框外边取消当前对话框
    }
    
    @Override
    protected void initData() {
       super.initData();
       convertView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }
    
    public void setObject(Object object) {
       mObject = object;
    }
    
    @Override
    protected int getLayoutRes() {
       return R.layout.dlg_point;
    }
    
    @Override
    public void onGlobalLayout() {
       int   heightPixels = getContext().getResources().getDisplayMetrics().heightPixels;
       int[] outLocation  = new int[2];
       convertView.getLocationOnScreen(outLocation);
       int y = outLocation[1] + convertView.getHeight();
       if (y != heightPixels) {
          KeyboardHelper.getIns().onKeyboardHeightChange(mObject, true, heightPixels - y);
       } else {
          KeyboardHelper.getIns().onKeyboardHeightChange(mObject, false, heightPixels - y);
       }
    }
    
    @Override
    protected void initViews(View convertView) {
       getWindow().getAttributes().width = WindowManager.LayoutParams.MATCH_PARENT;
       getWindow().getAttributes().gravity = Gravity.BOTTOM;
    }
    
    @Override
    public void dismiss() {
       super.dismiss();
       if (convertView != null) {
          convertView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
       }
    }
    }

    相关文章

      网友评论

          本文标题:关于软键盘的处理,理论上适应各种情况

          本文链接:https://www.haomeiwen.com/subject/ubvjmftx.html