java - 将光标设置为自动搜索 View

标签 java android searchview

我在 Activity 中有一个按钮,单击该按钮后,我会将用户定向到工具栏中带有搜索 View 的搜索 Activity 。

我正在使用 android.support.v7.widget.SearchView,我设法使用 onCreateOptionsMenu< 中的 searchView.setIconifiedByDefault(false) 自动展开它.. 我还在 onCreate Activity 中自动打开键盘,但我仍然必须在 searchView 中单击以开始在那里输入,我希望光标自动设置在那里..

有什么帮助吗?

最佳答案

调用setIconifiedByDefault(false)当搜索 View 被初始化时。

Sets the default or resting state of the search field. If true, a single search icon is shown by default and expands to show the text field and other buttons when pressed. Also, if the default state is iconified, then it collapses to that state when the close button is pressed. Changes to this property will take effect immediately.

试试下面的方法,它对我有用过一次。

   searchView.setIconifiedByDefault(true);
   searchView.setFocusable(true);
   searchView.setIconified(false);
   searchView.requestFocusFromTouch();

更新:如果您正在使用 android.support.v7.widget.SearchView行为非常不同。 clearFocus如果您不想一直弹出键盘,则需要。

Also in the searchview layout, use <requestFocus /> tag.

关于java - 将光标设置为自动搜索 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48039659/

相关文章:

java - JUnit、@ControllerAdvice 以及 Kotlin 中缺乏检查异常

java - CloudSim - 了解基本模拟结果

java - list 中的属性

java - 无法在 Android 搜索小部件中传递搜索变量

android - 操作栏中的 Xamarin Android 搜索 View 为空

java - @Past 还是今天的 Bean 验证?

java - 当我从服务器获取 JSON 字符串时,JSONArray 不起作用

java - Google Play 服务多人玩家的连接

java - 5 个按钮的一种布局

android - 使用搜索 View 过滤自定义列表项