2012-04-20 40 views
3

有一個頁面,用戶可以在需要時發送電子郵件,短信或呼叫其客人。問題在於,當用戶向其發送電子郵件時,鍵盤不會隱藏。即使 - 雖然我在解決問題時遇到了一個小問題,但似乎很難找到相似的帖子來解決問題。我也會製作截圖並放在這裏。如何在使用電子郵件發送電子郵件後自動隱藏鍵盤.Intent

enter image description here enter image description here enter image description here enter image description here

正如你可以看到,鍵盤不會隱藏發送郵件後。

+0

你發現了一個解? – Poutrathor 2013-11-21 17:19:36

+0

什麼新東西?,我有同樣的缺陷。 – Mikey 2014-06-18 08:52:03

回答

3

當鍵盤顯示並隱藏自身時,它不是重寫,但這裏是我用來根據需要隱藏和顯示鍵盤的兩種方法。

public void hideKeyboard(final View aView){ 
       aView.post(new Runnable() { 
        @Override 
        public void run(){ 

        InputMethodManager inputManager = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); 
        inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
        } 
       } 
    } 
    public void showKeyboard(final View aView) { 
     aView.post(new Runnable() { 
      @Override 
      public void run() { 

       InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
       inputMethodManager.toggleSoftInputFromWindow(ListingScreen.this.getCurrentFocus().getWindowToken(), InputMethodManager.SHOW_FORCED, 0); 
      } 
     }); 
    } 

當您調用hide/show鍵盤時,請傳入當前視圖。後運行的線程將等待運行,直到視圖完成加載,然後關閉鍵盤。

+0

我已經在onCreate和電子郵件意圖之後使用了您的hideKeyboard()方法。問題是它不起作用。發送電子郵件後,鍵盤似乎仍然處於活動狀態。 – Xarialon 2012-04-20 13:00:52

+0

試着把它放在postRunnable線程中。我將編輯我的代碼作爲示例 – coder 2012-04-20 13:02:16

0

調用這個方法在您想要如果打開隱藏鍵盤的地方(如調用此當您單擊以sendingEmail按鈕)

protected void showVirturalKeyboard() 
{ 

    Timer timer = new Timer(); 
    timer.schedule(new TimerTask() 
    { 
     @Override 
     public void run() 
     { 
      InputMethodManager m = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
      if(m != null) 
      { 
       m.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY); 
      } 
     } 
    }, 100); 
} 
+1

不,我不認爲有任何方法可以解決此問題。在發送電子郵件後,我需要訪問Gmail應用以隱藏鍵盤。 – Xarialon 2012-04-20 13:45:46

3

很容易只需添加下面的代碼在你的清單爲慾望活動:

android:windowSoftInputMode="stateAlwaysHidden" 
android:configChanges="keyboardHidden" 
+2

我做了,它沒有幫助。 – Xarialon 2012-04-26 19:32:33

+0

這是一個更優雅的解決方案,+1 – 2018-02-13 01:42:16

-1

我有類似的問題。 Gmail在發送後隱藏鍵盤。當你回到你的應用程序時,它會專注於其他事情。如果你使用較慢的設備,你會看到gmail在發送消息後隱藏鍵盤。

+0

顯然不是一個通用規則 – Poutrathor 2013-11-21 15:24:52

0

嘗試在StackOverflow上找到的每個解決方案後,似乎沒有任何工作。最後我確實找到了一種強制關閉鍵盤的方法,但這並不理想。

您可以在Android Manifest中爲該活動設置android:windowSoftInputMode =「adjustPan」。

這樣做的不好的副作用在這裏http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft解釋說:

「不調整大小活動的主窗口,以騰出空間給軟鍵盤相反,窗口中的內容會自動平移,使當前的焦點。永遠不會被鍵盤遮擋,用戶總是可以看到他們正在鍵入的內容,這通常不如調整大小,因爲用戶可能需要關閉軟鍵盤才能與窗口的隱蔽部分進行交互。「

6
Intent sendIntent = new Intent(Intent.ACTION_SEND); 
          sendIntent.setType("text/plain"); 
          sendIntent.putExtra(Intent.EXTRA_EMAIL, 
            new String[] { **EmailAddress** }); 
          startActivityForResult(sendIntent, 1); 




    @Override 
    protected void onActivityResult(int arg0, int arg1, Intent arg2) { 
     super.onActivityResult(arg0, arg1, arg2); 
     new Handler().postDelayed(new Runnable() { 
      @Override 
      public void run() { 
       InputMethodManager inputManager = (InputMethodManager) activity 
         .getSystemService(Context.INPUT_METHOD_SERVICE); 
       inputManager.hideSoftInputFromWindow(**AnyViewOfScreen**.getWindowToken(), 
         InputMethodManager.HIDE_NOT_ALWAYS); 
      } 
     }, 300); 
    } 
+0

感謝這段代碼,我在onResume方法中使用了代碼,它的工作方式就像一個魅力。 – Ganesh 2015-06-09 06:58:45

3

希望這可以幫助別人:

@Override 
protected void onResume() { 
    super.onResume(); 
    Log.d("OnResume", "Called"); 
    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      InputMethodManager inputManager = (InputMethodManager) LocationDetailActivity.this 
        .getSystemService(Context.INPUT_METHOD_SERVICE); 
      inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 
        InputMethodManager.HIDE_NOT_ALWAYS); 
     } 
    }, 300); 

} 

,如果你沒有在你的佈局的任何可獲得焦點視圖,只需添加一個虛擬的線性佈局,以你的XML

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:focusable="true" 
    android:focusableInTouchMode="true"> 

    <requestFocus /> 
</LinearLayout>