在Android 11
中,当targetSdk
设置为30
并启用语音搜索时,麦克风图标不会显示在SearchView
. 但是,如果targetSdk
设置为 29 ,它可以正常工作。它Android 10
也适用于带有targetSdk 30
.
有什么额外的事情需要做的30
吗?
菜单.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:title="Search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always" />
</menu>
清单.xml
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
</activity>
可搜索的.xml
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="Search"
android:imeOptions="actionSearch"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"/>
主要活动
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.search, menu)
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
val searchView: SearchView = menu.findItem(R.id.action_search).actionView as SearchView
val searchableInfo = searchManager.getSearchableInfo(componentName)
Log.d(TAG, "onCreateOptionsMenu: ${searchableInfo.voiceSearchEnabled}, ${searchableInfo.voiceSearchLaunchRecognizer}")
searchView.setSearchableInfo(searchableInfo)
searchView.setIconifiedByDefault(false)
return super.onCreateOptionsMenu(menu)
}