2011-02-25 102 views
1

我試圖使用Android內置的Search Dialog,並且它工作正常,除非我嘗試將其與ProgressDialog一起使用。他的基本事件鏈:Android搜索對話框軟鍵盤保持打開狀態的時間太長

  1. 用戶按搜索按鈕,輸入查詢並提交它。
  2. SearchManager調用搜索Activity的onCreate方法。
  3. 在onCreate方法中,我調用一個運行查詢的AsyncTask,並在onPreExecute方法中顯示ProgressDialog並將其隱藏在onPostExecute方法中。

    enter image description here

    ...這是相當難看:

這一切都爲我提交查詢的屏幕看起來是這樣的,除了儘快發生的罰款。我怎樣才能防止這種情況發生?

You can find the source code for the project at Google Code.

回答

0

你試過下面的代碼可能會幫助

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); 
+0

我試圖在運行AsyncTask之前在搜索活動中運行它,沒有運氣。 – cdmckay 2011-02-25 06:03:07

+0

在最終String查詢= intent.getStringExtra(SearchManager.QUERY)後放置此代碼;在您的搜索活動 – ingsaurabh 2011-02-25 06:09:11

+0

不起作用...還有一個問題是我的'myEditText'應該是什麼?我使用了'mListView',但我不確定這是否正確。 – cdmckay 2011-02-25 06:17:35

1

我碰到了同樣的問題來了,「解決」它在你的活動通過處理器

延緩進度對話框顯示,創建處理程序,只顯示以前創建的進度對話框:

private final Handler mHandler = new Handler() { 

    @Override 
    public void handleMessage(Message msg) { 
     if (null != mProgress) { 
      mProgress.show(); 
     } 
    } 
}; 

然後用下面的方法來顯示或隱藏:

private void showWaitPopup(final boolean doShow) { 
    if (doShow) { 
     if (null == mProgress) { 
      mProgress = new ProgressDialog(this); 
      mProgress.setMessage(getString(R.string.searching)); 
      mProgress.setCancelable(false); 

      // launch timer here 
      mHandler.sendEmptyMessageDelayed(0, 100); 
     } 
    } else { 
     if (null != mProgress) { 
      mProgress.dismiss(); 
      mProgress = null; 
     } 
    } 
} 
1

我有一個類似的問題與執行或取消搜索後隱藏鍵盤。問題是你無法獲得對SearchManager的搜索對話框或編輯文本的引用(所以你通常的hideSoftInputFromWindow方法將不起作用。)如果你運行下面的代碼作爲你的設置的一部分,它應該照顧你的問題:

SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
searchManager.setOnDismissListener(new OnDismissListener() { 

    @Override 
    public void onDismiss() { 
     final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
     imm.toggleSoftInput(0, 0); 
    } 
}); 

值得注意的是,在所有版本的Android上切換鍵盤可能都不是正確的做法。當OS版本是薑餅或更早版本時,我只運行這個代碼。否則,我使用SearchView。

1

我知道這是晚了,但它可能仍然有用。如果您使用SearchDialog你不能訪問其SearchManager的(如安卓2),可以關閉軟鍵盤有以下:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.toggleSoftInput(0, 0); 

,或者如果可以訪問編輯文本(例如搜索查看在安卓4.0或任何EditText):

imm.hideSoftInputFromWindow(YOUR_EDIT_TEXT.getWindowToken(), 0); 
相關問題