如果我将SearchView查询设置为比该框长的字符串,我该如何使它成为字符串的第一部分而不是最后一部分? (我将它用作WebView的导航/搜索栏。)
我做
mySearchView.setQuery("Start of long string.... at the end.", false);
并希望它显示
Start of long
代替
at the end
它总是向我展示结束。基本上,我希望它滚动回文本的开头。
编辑:我确实想要完整的文本,只是滚动到开头。
如果我记得很清楚你需要在EditText
中获得SearchView
,然后在上面设置椭圆形。
int id = mySearchView.getContext()
.getResources()
.getIdentifier("android:id/search_src_text", null, null);
EditText editText = (EditText) mySearchView.findViewById(id);
editText.setEllipsize(TextUtils.TruncateAt.END);
如果您不想进行椭圆化,可以使用相同的方法并在EditText中设置位置。
int id = mySearchView.getContext()
.getResources()
.getIdentifier("android:id/search_src_text", null, null);
EditText editText = (EditText) mySearchView.findViewById(id);
editText.setSelection(0);
基于Android: showing text in EditText from start,我会尝试这个
mySearchView.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus == false) { // lost focus
mEdittext.setSelection(0,0);
}
}
});
另外,检查这个Android SearchView OnFocusChangeListener: onFocusChange is not called at all,否则我无法帮助你
以上解决方案对我没有用。我正在使用appcompat v7操作栏,这很有效!
EditText editText = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
editText.setSelection(0);