2016-08-16 35 views
0

我按照下面的代碼打開: -鍵盤保持在工具欄返回箭頭按下

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // handle arrow click here 
     if (item.getItemId() == android.R.id.home) { 

      finish(); 
      overridePendingTransition(R.transition.right_in, R.transition.right_out); 


     } 
     return super.onOptionsItemSelected(item); 
    } 

在這個時候,我的鍵盤是開放的,我按我的工具欄回來arrrow,鍵盤保持開放和活動得到完成。我試圖強行callling以下方法上暫停()隱藏鍵盤,但並不好看,而轉變: -

public static void hideKeyboard(Activity activity) { 
     InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); 
     //Find the currently focused view, so we can grab the correct window token from it. 


     View view = activity.getCurrentFocus(); 
     //If no view currently has focus, create a new one, just so we can grab a window token from it 
     if (view == null) { 
      view = new View(activity); 
     } 
     imm.hideSoftInputFromWindow(view.getWindowToken(), 0); 

    } 

回答

2

嘗試到工具欄後退按鈕在把這個代碼:

//Hide keyboard when button was clicked. 
     InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); 
     inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0); 

像這樣:

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // handle arrow click here 
     if (item.getItemId() == android.R.id.home) { 

      finish(); 
      overridePendingTransition(R.transition.right_in, R.transition.right_out); 


     } 
    //Hide keyboard when button was clicked. 
     InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); 
     inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0); 
     return super.onOptionsItemSelected(item); 
    } 
+0

@ Adley-經過我和整理活動之前試過這個隱藏鍵盤,問題是當它返回到前一個畫面..它顯示鍵盤的一些空白區域,同時隱藏它,這看起來真的很難看,因爲用戶體驗。 –

+0

你測試過其他設備,物理設備嗎?這是奇怪的... – Adley

+0

是的,我已經嘗試與三星s6和摩托g2 ..但結果是相同的,這很奇怪。 –

0

你爲什麼要拿到安卓主屏按鈕onOptionsItemSelected()? 只是做如上:

toolbar.setNavigationOnClickListener(new Button.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      main = new Intent(SettingActivity.this, MainActivity.class); 
      main.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); 
      finish(); 

      InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE); 
      inputMethodManager.hideSoftInputFromWindow(v.getWindowToken(), 0); 

      startActivity(main); 
      overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); 
     } 
    }); 

希望它會幫助你,Darkball60