能否在不显示键盘的情况下扩展SearchView?

4

我在操作栏中有一个SearchView。当用户单击它时,它会展开并出现键盘。这是预期的行为。然而,在我的应用程序中,有一种情况是我通过编程方式扩展SearchView。

MenuItem searchMenuItem = menu.findItem(R.id.action_search);
MenuItemCompat.expandActionView(searchMenuItem);

在这种情况下,我不想显示键盘。我通过编程方式扩展SearchView,只是为了向用户展示上次的搜索查询。

那么有没有可能打开搜索视图而不显示键盘呢?


1
你找到解决方案了吗? - gersonmdesouza
@gersonmendes,我没有找到解决方案。但是如果我再次使用SearchView,我会搜索一下Play Store中使用的那个复杂视图。它比通常的SearchView要好得多。 http://www.androidcentral.com/sites/androidcentral.com/files/styles/larger_wm_brw/public/article_images/2015/03/google-play-update.JPG?itok=sIj0Zj7E - Egis
4个回答

4
我通过以下方法解决了我的问题:

我使用了这个:

@Override
    public void onPrepareOptionsMenu(Menu menu) {
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.menu_search));
        if(!TextUtils.isEmpty(mSearch)){
            searchView.setQuery(mSearch, false);
            searchView.setIconified(false);
            searchView.clearFocus();
        }
    }

请问这段代码解决了你的问题吗?

1
清晰的答案:searchView.setIconified(false);searchView.clearFocus(); 的组合。 - johnkarka

1

我使用以下代码扩展搜索视图而不打开键盘:

searchView.setFocusable(false);
searchView.setIconified(false);
searchView.clearFocus();

希望这能帮助到某人。

0
我已经解决了这个问题。请按照以下代码覆盖该方法--
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    MenuItem menuItem = menu.findItem(R.id.menu_search);

    searchView = ((SearchView) menuItem.getActionView());

    searchView.setFocusable(true);
    searchView.setIconified(false);
    searchView.requestFocusFromTouch();
    menuItem.setVisible(true);

}

0
在我的应用程序中,我想要一个符合以下要求的SearchView:
  1. SearchView默认展开,用户无法折叠
  2. 键盘不会自动弹出,只有当用户点击SearchView时才会弹出
我使用以下代码成功实现了预期结果。

activity_search.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#ffffff"
        android:elevation="8dp"
        app:contentInsetStartWithNavigation="0dp">

        <androidx.appcompat.widget.SearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusable="false"
            app:iconifiedByDefault="false"
            app:queryBackground="@android:color/transparent"
            app:queryHint="Search..." />

    </androidx.appcompat.widget.Toolbar>

</LinearLayout>

SearchActivity.kt

class SearchActivity : AppCompatActivity(), SearchView.OnQueryTextListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_search)
        setSupportActionBar(toolbar)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        search_view.setOnQueryTextListener(this)
    }

    override fun onQueryTextChange(newText: String?): Boolean {
        // Perform search either here or in onQueryTextSubmit()
        return true
    }

    override fun onQueryTextSubmit(query: String?): Boolean {
        // Clear focus here to avoid situation where in order to exit the activity you have to click Back twice
        // This happens if you click "Search" button on your keyboard and then want to exit the activity.
        // First Back click clears SearchView focus while the second click exits the activity.
        // You don't want to click twice, do you?
        search_view.clearFocus()
        return false
    }

    override fun onOptionsItemSelected(item: MenuItem) = when (item.itemId) {
        android.R.id.home -> { onBackPressed(); true }
        else -> super.onOptionsItemSelected(item)
    }

}

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接