您的位置:首页 > 其它

AlertDialog无法弹出软键盘问题

2017-09-20 17:51 211 查看
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 != 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: