Android判斷軟鍵盤是否顯示
阿新 • • 發佈:2019-01-26
如果顯示則隱藏 否則彈出彈窗
if (isSoftShowing()) { final View v = (this).getWindow().peekDecorView(); if (v != null && v.getWindowToken() != null) { InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(v.getWindowToken(), 0); } } else{ CardPopuUtil.getInstance().showPopu(tvMeBackcardCardtype, tvMeBackcardCardtype, this); }
判斷是否顯示方法
private boolean isSoftShowing() {
//獲取當螢幕內容的高度 int screenHeight = this.getWindow().getDecorView().getHeight(); //獲取View可見區域的bottom Rect rect = new Rect(); //DecorView即為activity的頂級view this.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);//考慮到虛擬導航欄的情況(虛擬導航欄情況下:screenHeight = rect.bottom + 虛擬導航欄高度) //選取screenHeight*2/3進行判斷 return screenHeight*2/3 > rect.bottom; }