Android searchView,显示长字符串的第一部分而不是最后一部分

Android searchView, Show first part of long string instead of last

如果我将 SearchView 查询设置为比框长的字符串,我如何才能使字符串的第一部分而不是最后一部分可见? (我将其用作 WebView 的 navigation/search 栏。)

我愿意

mySearchView.setQuery("Start of long string.... at the end.", false);

并希望它显示

Start of long

而不是

at the end

它总是让我看到结局。我,本质上,希望它滚动回到文本的开头。

编辑:我确实想要完整的文本,只是滚动到开头。

基于 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,否则帮不了你

如果我没记错的话,你需要抓住 SearchView 中的 EditText,然后在其上设置椭圆大小。

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);

以上解决方案对我不起作用。 我使用的是 appcompat v7 操作栏,这很有效!

EditText editText = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
editText.setSelection(0);