檢出以下內容: 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。 – dbDev
當你說「它不工作」,你的意思是你沒有看到日誌記錄或手機仍然啓動搜索? – DeeV
正確,我沒有看到日誌沒有onCreate()onSearchRequested()。搜索沒有啓動,但我的對話消失了。 –