Android 是否可以在不显示键盘的情况下展开SearchView

Android 是否可以在不显示键盘的情况下展开SearchView,android,keyboard,searchview,Android,Keyboard,Searchview,我在行动栏中有SearchView。当用户单击它时,它会展开,然后出现键盘。这是预期的行为。但是,在我的应用程序中,有一种情况是我以编程方式扩展SearchView MenuItem searchMenuItem = menu.findItem(R.id.action_search); MenuItemCompat.expandActionView(searchMenuItem); 在这种情况下,我不想显示键盘。我以编程方式展开SearchView只是为了向用户显示上一个搜索查询是什么 那么,

我在行动栏中有SearchView。当用户单击它时,它会展开,然后出现键盘。这是预期的行为。但是,在我的应用程序中,有一种情况是我以编程方式扩展SearchView

MenuItem searchMenuItem = menu.findItem(R.id.action_search);
MenuItemCompat.expandActionView(searchMenuItem);
在这种情况下,我不想显示键盘。我以编程方式展开SearchView只是为了向用户显示上一个搜索查询是什么


那么,是否可以在不显示键盘的情况下打开搜索视图?

我使用以下方法解决问题:

@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();
        }
    }

请告诉我此代码是否解决了您的问题。

我已解决此问题。请按以下代码重写该方法--


我使用以下代码在不打开键盘的情况下展开searchview:

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

希望这对某人有所帮助。

在我的应用程序中,我希望有一个满足以下要求的SearchView:

  • 默认情况下,SearchView是展开的,用户不能折叠它
  • 键盘不会自动显示,但仅当用户单击SearchView时才会显示
  • 我设法用这个达到了预期的效果

    activity\u 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>
    

    你找到解决办法了吗?@gersonmendes,我没有找到解决办法。但如果我不得不再次使用SearchView,我会在谷歌上搜索Play Store中使用的详细视图。它比通常的SearchView要好得多。明确回答:
    searchView.setIconified的组合(false)
    searchView.clearFocus()
    
    <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>
    
    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)
        }
    
    }