欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 監(jiān)聽軟鍵盤狀態(tài)的實例詳解

 更新時間:2017年09月06日 08:57:00   作者:yellowcath  
這篇文章主要介紹了Android 監(jiān)聽軟鍵盤狀態(tài)的實例詳解的相關(guān)資料,希望通過本文能掌握這樣的知識,需要的朋友可以參考下

Android 監(jiān)聽軟鍵盤狀態(tài)的實例詳解

近日遇到要檢測軟鍵盤是否顯示或隱藏的問題,搜了一下網(wǎng)上,最后找到一個很簡單的,記錄一下。

activityRoot是activity的根view,就是xml里面的第一個view,給它設(shè)置一個id.

final View activityRootView = findViewById(R.id.activityRoot); 
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
  @Override 
  public void onGlobalLayout() { 
    int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight(); 
    if (heightDiff > 100) { // 如果高度差超過100像素,就很有可能是有軟鍵盤... 
      ... do something here 
    } 
   } 
}); 

但是這個依賴于Android:windowSoftInputMode="adjustResize",

如果是adjustPan,Activity高度不變就沒用了。然后終于給我找到一個界面高度不變也能用的辦法,這個辦法不能監(jiān)聽只能主動去查詢,不過正好我也只需要這樣就行了

InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);  
if(imm.hideSoftInputFromWindow(editText.getWindowToken(), 0)) 
{ 
   imm.showSoftInput(editText,0); 
//軟鍵盤已彈出 
} 
else 
{ 
  //軟鍵盤未彈出 
} 

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

最新評論