55

您好,我需要在 searchView 中放置一些文本并聚焦/展开 searchView 小部件。

这是我尝试过的,但它不起作用

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
...
    SearchView searchView = (SearchView) menu.findItem(R.id.menu_search).getActionView();
    searchView.setQuery(this.filtro, false);
    searchView.setFocusable(true);
    searchView.requestFocus();
    searchView.requestFocusFromTouch();
4

11 回答 11

124

尝试使用searchView.setIconified(false). 这对我有用:

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ActionBar actionBar = getActionBar();
    SearchView searchView = new SearchView(this);
    actionBar.setCustomView(searchView);
    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    searchView.setQuery("test",true);
    searchView.setFocusable(true);
    searchView.setIconified(false);
    searchView.requestFocusFromTouch();
}
于 2012-04-25T14:12:58.103 回答
4

这是另一个解决方案。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    MenuItem menu_search = menu.findItem(R.id.menu_search);


    menu_search.setOnActionExpandListener(new OnActionExpandListener() {
        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // Do something when collapsed
            return true;  // Return true to collapse action view
        }

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            //get focus
            item.getActionView().requestFocus();
            //get input method
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
            return true;  // Return true to expand action view
        }
    });
    return true;
}
于 2012-12-17T13:52:16.303 回答
2

这对我有用:

        @Override
        public boolean onMenuItemActionExpand(MenuItem item)
        {
            if (sm.isMenuShowing()) {
                sm.toggle();
            }
            searchView.setIconified(true);
            KeyEvent event = new KeyEvent(KeyEvent.ACTION_DOWN, 32);
            searchView.onKeyDown(32, event);
            return true;
        }
于 2013-10-22T17:50:26.877 回答
1

对我来说,以下作品:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    ...
    MenuItem miSearch = menu.findItem(R.id.menu_search);
    SearchView mSearchView = (SearchView) miSearch.getActionView();

    mSearchView.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mSearchView.setFocusable(true);
            mSearchView.requestFocusFromTouch();
        }
    });

我使用 android.support.v7.widget.SearchView。当试图设置mSearchView.setIconified(false);它进入一个无限循环。

于 2016-12-28T11:08:25.437 回答
0
searchView.setQuery("typeAnything",true);
searchView.setFocusable(true);

会成功的。

于 2015-12-14T08:06:28.257 回答
0
public void setIconified(boolean iconify) {
    if (iconify) {
        onCloseClicked();
    } else {
        onSearchClicked();
    }
}

这是 SearchView 中的代码

setIconified()对我有用

于 2018-04-16T02:43:39.563 回答
0

在活动 xml 中,您可以使用这一行:

android:iconifiedByDefault="false"
于 2018-06-04T01:56:42.777 回答
0

如果 SearchView 是通过菜单配置制作的,我使用菜单项展开操作视图。

private var searchMenuItem: MenuItem? = null

// create search menu item
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
    menuInflater.inflate(R.menu.search_menu, menu)
    searchMenuItem = menu?.findItem(R.id.action_search)
    val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
    (searchMenuItem?.actionView as? SearchView)?.apply {
        setSearchableInfo(searchManager.getSearchableInfo(componentName))
    }
    return super.onCreateOptionsMenu(menu)
}


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_search)
    setSupportActionBar(findViewById(R.id.toolbar))
    // Execute 'expandActionView' delayed 
    // because I want to focus field only when first start.
    Handler().postDelayed({
        // show field and focus it.
        searchMenuItem?.expandActionView()
    }, 500)
}
于 2019-02-08T18:20:01.990 回答
0

我在 onCreateOptionMenu 中实现了它并且它有效。你不需要使用setIconified(false);

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);

        // Associate searchable configuration with the SearchView
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setMaxWidth(Integer.MAX_VALUE);
        searchView.setOnClickListener(view -> menu.findItem(R.id.action_sleep_timer).setVisible(false));
        searchView.setOnCloseListener(() -> {
            menu.findItem(R.id.action_sleep_timer).setVisible(true);
            return false;
        });
        searchView.setOnSearchClickListener(view -> {
            menu.findItem(R.id.action_sleep_timer).setVisible(false);
            searchView.requestFocus();
        });
return true;
    }
于 2019-04-24T02:09:53.443 回答
0

使用 MenuItem 的 expandActionView()

override fun onCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.menu_search, menu)
        menuItemSearch = menu.findItem(R.id.action_search)
        searchView = menuItemSearch?.actionView as SearchView
        // ...
        menuItemSearch?.expandActionView()
        return true
    }

你需要添加这一行:

menuItemSearch?.expandActionView()
于 2021-06-04T10:01:08.153 回答
0

对我来说,@farmer.chs 的方法只有在我添加延迟后才有效:

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
 super.onViewCreated(view, savedInstanceState)

 activity?.let {
            searchBar?.postDelayed({
                  searchBar.isIconified = false
                  searchBar.isFocusable = true
                  searchBar.requestFocusFromTouch()
            }, 100)
        }

PS这个代码在片段中被调用

于 2022-02-22T09:45:46.600 回答