Search Dialog in Mono Android

2019-04-25 07:43发布

问题:

I'm trying to implement a search dialog in a Mono Android app per the documentation here: http://developer.android.com/guide/topics/search/search-dialog.html

I have an activity that the user should be able to search from:

[Activity (Label = "MyActivity", MainLauncher = true, Icon = "@drawable/icon", Theme = "@style/MyStyle")]
[MetaData ("android.app.default_searchable", Value = ".SearchActivity")]
public class MainActivity : BaseActivity {...

I have a searchable activity (where the heavy-lifting will happen):

[Activity(Theme = "@style/MyStyle", Label = "Searchable", LaunchMode = Android.Content.PM.LaunchMode.SingleTop)]
[IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { Intent.CategoryLauncher, Intent.ActionSearch })]
[MetaData("searchable", Resource = "@xml/searchable")]
public class SearchActivity : BaseActivity { ...

And I have my searchable.xml:

<?xml version="1.0" encoding="utf-8"?>
<searchable
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:label="MyLabel"
  android:hint="Search Products">
</searchable>

When I press the search key on the phone in the MainActivity, nothing happens - no dialog. I think my problem lies with how the attributes are being translated into the AndroidManifest.xml at runtime but I'm not sure.

UPDATE 1/3/2012: I have posted a project distilled down to the most basic elements here. Press the search button on your Android and you should see the SearchDialog but it doesn't appear: Demo Project Here

回答1:

The problem is in the [MetaData] attribute on MainActivity. If you provide the properly namespaced version of the class the search dialog appears correctly:

[MetaData ("android.app.default_searchable", Value = "searchdialogtest.SearchActivity")]
public class MainActivity : BaseActivity {


回答2:

in your serachable.xml you can't have a constant string as a name and hint ... they should be linked dynamically @string/my_app_name @string/my_hint

and all work's fine!



回答3:

I feel In the manifest file, the searachable meta-data tag is not proper (android:name="android.app.searchable")

Sample Manifest - MainActivity Tag -

<activity android:name=".MainActivity"> <meta-data android:name="android.app.default_searchable" android:value="pckname.SearchClass"/> </activity>

SearchActivity tag -

<activity android:name=".SearchActivity" >
<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>