SearchView - 选择并设置建议时显示文本的开头

SearchView - Show starting of the text when suggestion is selected and set

我想这一定很简单,但我自己想不出来。

我的应用程序中有一个搜索视图,在输入时会给出一些建议。从下拉列表中选择(点击)特定建议时,建议文本将设置到搜索视图中,但 focus/cursor 位于文本末尾。下图可以看到光标在最后。

有没有办法在搜索视图中将光标设置到文本的开头?这样当设置文本时,显示文本的开始部分而不是结束部分。

谢谢。

1 - 实际上 searchView 使用 textview 作为我们设置查询的编辑文本。一个EditText就是一个TextView,它继承了TextView。所以首先找到那个 textview 的 id,如下所示。

TextView searchText = (TextView) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);

或使用此方法

int id = searchView.getContext()
               .getResources() 
               .getIdentifier("android:id/search_src_text", null, null); 
         EditText editText = (EditText) searchView.findViewById(id);

2 - 现在使用此编辑文本搜索您的数据。

3 - 获得查询结果后,您可以使用 editText.setSelection(0) 方法将 focus/cursor 设置为 0 位置....

对我有用,希望对你也有帮助....