Android監聽鍵盤打開或關閉

1.AndroidManifes 當前Activity下添加屬性
 android:windowSoftInputMode="stateAlwaysHidden|adjustResize"

2.Oncreate調用

 private void listenKeyboard() {
        // TODO Auto-generated method stub
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
        detailMainRL = (LinearLayout) findViewById(R.id.root_layout);
        detailMainRL.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener(){
                    @Override
                    public void onGlobalLayout()
                    {
                        int heightDiff = detailMainRL.getRootView().getHeight() - detailMainRL.getHeight();
                        if (heightDiff > detailMainRL.getRootView().getHeight()/3)
                        { // 說明鍵盤是彈出狀態
                            Toast.makeText(MainActivity.this, "open", Toast.LENGTH_SHORT).show();
                        } else{
                            Toast.makeText(MainActivity.this, "close", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }
  注:detaiMainRL爲根佈局id

疑問:當佈局中設置狀態欄,導航欄時,獲取鍵盤高度失效,有知道的大神,請解答下


發佈了88 篇原創文章 · 獲贊 18 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章