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

2018-07-23  本文已影响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);
   }
}
}
上一篇下一篇

猜你喜欢

热点阅读