2012-10-31 36 views
8

檢出以下內容: How to have clicking the phone's search button do nothing?Android - 如何禁用搜索按鈕,如何實現onSearchRequested()?

但它不適用於我。任何人有任何其他建議?謝謝。 根據Phil的建議,根據他的回答更新代碼。 顯示bug http://www.youtube.com/watch?v=9lekcH1JAf0&feature=youtu.be

所以我做了一個示例項目。我已經在兩個不同的手機上測試過這個版本,Android版本2.3.5和4.1,我也在模擬器版本2.2上測試了這個。當我點擊按鈕顯示對話框後,它顯示進度對話框很好,它應該繼續這樣做。但單擊電話和模擬器上的搜索按鈕會使進度對話框消失。這裏是班級,清單如下。

當前的行爲是,當點擊搜索按鈕時,進度對話框消失。 預期或需要的行爲是禁用搜索按鈕,或類似地,當顯示對話框並單擊搜索按鈕時,該點擊不會使對話消失。

package com.example.test6; 

import android.os.Bundle; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.util.Log; 
import android.view.KeyEvent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainActivity extends Activity { 

    Button showDialogButton; 
    Context context; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     showDialogButton = (Button) findViewById(R.id.showDialogButton); 
     showDialogButton.setOnClickListener(showDialog); 
     context = this; 
     startSearch(null, false, null, false); 
    } 

    private OnClickListener showDialog = new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      ProgressDialog progressDialog = new ProgressDialog(context); 
      progressDialog.show(); 
     } 
    }; 

    @Override 
    public boolean onSearchRequested() { 
     Log.d("onSearchRequested", "search key pressed"); 
     return false; 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 
} 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.test6" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="1" 
     android:targetSdkVersion="15" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="Test6" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
     <meta-data 
      android:name="android.app.searchable" 
      android:resource="@xml/searchable" /> 
     </activity> 
    </application> 

</manifest> 

根據所提供的參考,我可搜索的xml位於:res/xml/searchable.xml。這是我的searchable.xml看起來像:

<?xml version="1.0" encoding="utf-8"?> 
<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
    android:searchSuggestAuthority="dictionary" 
    android:searchSuggestIntentAction="android.intent.action.VIEW" 
    android:includeInGlobalSearch="true"> 
</searchable> 
+1

向我們展示您的代碼,這是建議#1。 – dbDev

+0

當你說「它不工作」,你的意思是你沒有看到日誌記錄或手機仍然啓動搜索? – DeeV

+0

正確,我沒有看到日誌沒有onCreate()onSearchRequested()。搜索沒有啓動,但我的對話消失了。 –

回答

4

編輯

很高興我下面的代碼是有幫助的。隨着更新的問題(與視頻一起),我現在相信這是你需要的答案:Prevent ProgressDialog from being dismissed when I click the search button (Android)

- PRE-EDIT -

呼籲從onCreate()onSearchRequested()的相反,你需要調用:

startSearch(null, false, null, false); 

然後,爲了防止搜索按鈕使用的onSearchRequested()方法,您可以覆蓋它並返回false:

@Override 
public boolean onSearchRequested() 
{ 
    return false; 
} 

該解決方案在onSearchRequested documentation直接討論:

您可以使用此功能作爲一種簡單的方式來啓動搜索UI,在中 響應菜單項,搜索按鈕或其他部件的 活動。調用此函數與調用startSearch(null,false,null,false)的 相同,該函數按照其清單中指定的當前活動啓動搜索 ,請參閱 SearchManager。

您可以覆蓋此功能以強制執行全局搜索,例如,在 響應一個專用的搜索鍵,或完全阻止搜索(通過 簡單地返回false)。

您的AndroidManifest.xml也是不完整的。你需要證明你的Activity是可搜索的。更改MainActivity聲明如下:

<activity 
     android:name=".MainActivity" 
     android:label="@string/title_activity_main" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
     <meta-data 
      android:name="android.app.searchable" 
      android:resource="@xml/searchable" /> 
    </activity> 

注意@ XML /搜索是您的APK的XML參考,定義瞭如何進行搜索。此資源詳細討論here

+0

謝謝菲爾,我覺得我(我們)很親密。但仍然存在這個問題。我在那裏做錯了什麼?更新了代碼,附上了視頻。 –

+0

@NomanArain我做了一個編輯,我認爲這會導致你解決你的問題 – Phil

+0

我看不到有什麼變化。你是否指向你提供的鏈接? –

0
@Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_SEARCH) { 
      Log.d("onKeyDown", "search key pressed"); 
      // onSearchRequested(); 
      return true; // You need to return true here. This tells android that you consumed the event. 
     } 
     return false; 
    } 
+0

此代碼尚未解決問題。 –

+0

我似乎無法重現您的問題。 –

+0

你正在使用我上面發佈的代碼?你在我提到的模擬器上試過這個嗎? –

0

在您的活動不能中斷搜索關鍵事件。

您必須中斷對話框中的搜索關鍵事件。

private OnClickListener showDialog = new OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     ProgressDialog progressDialog = new ProgressDialog(context); 
     progressDialog.setOnKeyListener(new OnKeyListener(){ 
       public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)  { 
       if (keyCode == KeyEvent.KEYCODE_SEARCH) { 
        Log.d("onKeyDown", "search key pressed"); 
        return true; 
       } 
       return super.onKey(dialog,keyCode,event); 
    }}); 
     progressDialog.show(); 
    } 
}; 
+0

我不知道我是否不理解這個? http://developer.android.com/reference /android/app/Activity.html#onSearchRequested%28%29 「您可以覆蓋此功能以強制執行全局搜索,例如響應專用搜索鍵,或者完全禁止搜索(通過簡單地返回false)。」 搜索for onSearchRequested在上面的頁面。 –

+0

@Noman Arain,你有嘗試過我的建議嗎?這是關於關鍵事件調度。 ProgressDialog也是一個窗口。所以它有keydispatching本身。所以你應該在對話框中而不是在活動中中斷搜索鍵。 – landry