2016-10-03 127 views
0

我想在軟鍵盤彈出窗口中隱藏操作欄。我已經使用下面的代碼來獲得結果的代碼。但是當我想回去時,應用程序崩潰。我已經包括在哪一行,我也收到了錯誤。請通過這個指導我。在軟鍵盤上隱藏操作欄彈出片段

在此先感謝。

私人無效CreateView的(){

mRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 

     @Override 
     public void onGlobalLayout() { 

      Rect rect = new Rect(); 
      //rect will be populated with the coordinates of your view that area still visible. 

      mRootView.getWindowVisibleDisplayFrame(rect); 
      int heightDiff = mRootView.getRootView().getHeight() - (rect.bottom - rect.top); 

      if (heightDiff > 100) { 

       // if more than 100 pixels, its probably a keyboard... 
       //keyboard visible 
       mBinding.healthRecordContainer.setVisibility(View.GONE); 
       ((AppCompatActivity) getActivity()).getSupportActionBar().hide(); 

      } else { 

       //keyboard not visible 
       mBinding.healthRecordContainer.setVisibility(View.VISIBLE); 
       ((AppCompatActivity) getActivity()).getSupportActionBar().show(); // app crashes 
      } //java.lang.NullPointerException 
     } 
    }); 
} 
+0

試一下:[http://stackoverflow.com/questions/15327208/how-to-show-hide-actionbar-when-clicked-on](http://stackoverflow.com/questions/15327208/how-to-show-hide-actionbar-when-clicked-on) –

回答

0

您可以使用下面的代碼。然後,應用程序不會崩潰。

Activity activity = getActivity(); 

if (activity instanceof ActionBarActivity) { 

    ActionBarActivity actionBarActivity = (ActionBarActivity) activity; 

    ActionBar actionBar = actionBarActivity.getSupportActionBar(); 

    if (actionBar != null) { 
     actionBar.hide(); 
    } 
} 
+0

謝謝你的回答。 – Harshith

0

只做一件事情在你的清單,你必須聲明你的活動。例如對於例如

<activity 
      android:name=".Your Activity here" 
      android:windowSoftInputMode="stateVisible|adjustResize" 
      > 
     </activity> 
+0

這是爲了隱藏軟鍵盤嗎? – Harshith