在 Android 中显示搜索栏

发布于 2024-10-05 04:49:25 字数 1384 浏览 0 评论 0原文

我在尝试在应用程序顶部显示搜索栏时遇到问题。我只是希望这个栏对于洞应用程序(在所有活动中)可见,即使应用程序启动时,也不需要用户按下按钮,我正在尝试使用 android 的可搜索功能。这是我到目前为止所得到的:

ma​​nifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Main"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".BigImageView">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
               android:resource="@xml/searchable"/>
    </activity>
    <!-- declare the default searchable Activity for the whole app -->
    <meta-data android:name="android.app.default_searchable"
           android:value=".BigImageView" />

</application>

res/xml/searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" >

我可以完成此操作吗?或者用户是否需要按按钮来调用搜索栏?

I'm having trouble when trying to show the search bar at the top of my app. I just want this bar to be visible for the hole application (in all the activities) even when the app starts, with no need for the user to press a button, I'm trying to use android's searchable function. Here's what I've got so far:

manifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".Main"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".BigImageView">
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
        <meta-data android:name="android.app.searchable"
               android:resource="@xml/searchable"/>
    </activity>
    <!-- declare the default searchable Activity for the whole app -->
    <meta-data android:name="android.app.default_searchable"
           android:value=".BigImageView" />

</application>

res/xml/searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/app_name"
android:hint="@string/search_hint" >

Can I accomplish this or does the user need to press a buttons to invoke the search bar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

羁〃客ぐ 2024-10-12 04:49:25

您可以放入

onSearchRequested();

onResume() - 但如果另一个组件获得焦点,搜索栏将隐藏。

如果这不是您想要的,您应该实现自己的搜索栏(即使用 AutoCompleteTextView)。

You could put

onSearchRequested();

in onResume() - but the search bar would hide if another component will get the focus.

If that's not what you want, you should implement your own search bar (i.e. with AutoCompleteTextView).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文