【问题标题】:How to open android search without click?如何在不点击的情况下打开android搜索?
【发布时间】:2015-05-26 11:04:13
【问题描述】:

我想要打开工具栏中的 android 搜索视图而不点击搜索图标。这是 XML 代码

<item
    android:id="@+id/action_search"
    android:title="@string/search_title"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always"/>

以及 MainActivity 中的 Java 代码。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);

    MenuItem menuItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
    searchView.setQueryHint("Type something...");
    int searchPlateId = searchView.getContext()
            .getResources()
            .getIdentifier("android:id/search_plate", null, null);
    View searchPlate = searchView.findViewById(searchPlateId);
    if (searchPlate != null) {
        searchPlate.setBackgroundColor(Color.DKGRAY);
        int searchTextId = searchPlate.getContext()
                .getResources()
                .getIdentifier("android:id/search_src_text", null, null);
        TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
        if (searchText != null) {
            searchText.setTextColor(Color.WHITE);
            searchText.setHintTextColor(Color.WHITE);
        }
    }
    return true;
}

这可行,下面的输出屏幕...

所以,当我点击搜索图标后,它会在下面输出屏幕...

我要的直接就是第二屏!

【问题讨论】:

  • 在第二个onCreateActivitysearch.performClick();
  • @Logic 自动调用onclik 但是.. 为什么你不直接在工具栏上放一个Edittext 并且不“搜索图标”?你也可以在你的编辑文本上添加一个图标
  • @Aspicas EditText 需要设计为看起来像默认的Search,而不是使用availbale 更容易和首选
  • 那么...我同意@Logic,他的解决方案是可行的。
  • @Madan Sapkota 我在这里也有类似的问题:stackoverflow.com/questions/43702055/…。关于如何解决的任何想法或想法?

标签: android searchview android-menu android-toolbar


【解决方案1】:

在初始化SearchView后尝试将此行添加到您的代码中:

searchView.setFocusable(true);

更新:

searchView.setIconified(false) worked 

【讨论】:

  • 那么这个searchView.setIconified(false); ?根据这个 SO question,它应该是你正在寻找的 stackoverflow.com/questions/14235677/…
  • searchView.setIconified(false) 有效!感谢您的帮助。
  • @Gabriella Angelova 我在这里也有类似的问题:stackoverflow.com/questions/43702055/…。关于如何解决的任何想法或想法?
  • 您的代码只是隐藏了 searchView 图标,它不会让它消失。
  • 如何关闭键盘,但仍然打开
猜你喜欢
  • 2021-01-17
  • 1970-01-01
  • 2018-06-05
  • 2014-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多