searchview在FIRST背面没有正确关闭(它只是失去焦点)

时间:2013-09-20 19:29:40

标签: android android-actionbar actionbarsherlock searchview

我必须按两次后退按钮才能关闭SearchView。为什么?在第一次按下时,SearchView只会失去焦点......

setOnKeyListener上设置SearchView也不起作用...

是的,我正在使用ABS实施......

我的代码很简单,如下所示:

mMenuItemSearch = menu.findItem(R.id.search);
mSearchView = new SearchView(getSupportActionBar().getThemedContext());
mMenuItemSearch.setActionView(mSearchView);

mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
{
    public boolean onQueryTextChange(String newText)
    {
        mPagerManager.getFragment(mSelectedPos).adapter.getFilter().filter(newText);
        return true;
    }

    public boolean onQueryTextSubmit(String query)
    {
        return true;
    }
});

11 个答案:

答案 0 :(得分:1)

在尝试了很多之后,我找到了解决方案。

onQueryTextSubmit(String query)中写searchView.clearFocus();

然后在onBackPressed()中使用以下代码:

        if (!searchView.isIconified())
        {
            searchView.setIconified(true);
            searchView.onActionViewCollapsed();
        }
        else
        {
            super.onBackPressed();
        }

答案 1 :(得分:1)

onBackPressed应该只清除搜索视图中的焦点(SearchAutoComplete)EditText,这是正常的行为。

但是如果你想改变这种行为,那就去做吧:(

    final SearchView.SearchAutoComplete searchAutoComplete = (SearchView.SearchAutoComplete) searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
    final MenuItem searchViewItem = menu.findItem(R.id.searchView);

    searchAutoComplete.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(!searchAutoComplete.isFocused()){
                searchViewItem.collapseActionView();
                searchView.onActionViewCollapsed();
                viewManager.dismissSearchDropDownPopupWindow();
            }
        }
    });

答案 2 :(得分:0)

关闭后的搜索视图是预期的行为以及我们习以为常的行为。如果您希望通过扩展自己的SearchView和/或监听onBackPressed()来调用SearchView所在的活动,您可以覆盖它。

答案 3 :(得分:0)

我这样解决了:

...
public class MyFragment extends Fragment implemented SearchView.OnFocusChangeListener    <----
...

@Override
public void onFocusChange(View v, boolean hasFocus) {
    MenuItem menuItem = menu.findItem(R.id.action_search);
    SearchView sv = (SearchView)MenuItemCompat.getActionView(menuItem);
    if(!hasFocus) {
        sv.onActionViewCollapsed();   <----
        interactionListener.showDrawerToggle(true);    <----
    }
}

...

public void configureSearchView(Menu menu) {
    this.menu = menu;
    MenuItem menuItem = menu.findItem(R.id.action_search);
    SearchView sv = (SearchView)MenuItemCompat.getActionView(menuItem);
    sv.setOnSearchClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            interactionListener.showDrawerToggle(false);
        }
    });

    sv.setOnQueryTextListener(this);
    sv.setOnCloseListener(this);
    sv.setSubmitButtonEnabled(false);
    sv.setIconifiedByDefault(true);
    sv.setOnQueryTextFocusChangeListener(this);     <----

    if(initialQuery != null) {
        sv.setIconified(false);
        menuItem.expandActionView();
        sv.setQuery(initialQuery, true);
    }
}
...

答案 4 :(得分:0)

在configureSearchView()方法中尝试这个,它对我有用。

    mSearchView.post(new Runnable() {
        @Override
        public void run() {
            mSearchView.setIconified(false);
        }
    });

答案 5 :(得分:0)

解决此问题的唯一方法是使用Fatal Exception: java.lang.NoClassDefFoundError: Failed resolution of: Lcom/noodoe/sunray/settings/preferences/gallery/GalleryActivity; at com.noodoe.sunray.settings.preferences.PreferencesActivity.lambda$setupListener$9(PreferencesActivity.java:248) at com.noodoe.sunray.settings.preferences.PreferencesActivity.access$lambda$9(PreferencesActivity.java) at com.noodoe.sunray.settings.preferences.PreferencesActivity$$Lambda$10.onClick(Unknown Source) at android.view.View.performClick(View.java:5226) at android.view.View$PerformClick.run(View.java:21266) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:168) at android.app.ActivityThread.main(ActivityThread.java:5845) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) Caused by java.lang.ClassNotFoundException: Didn't find class "com.noodoe.sunray.settings.preferences.gallery.GalleryActivity" on path: DexPathList[[zip file "/data/app/com.noodoe.sunray.alpha-1/base.apk"],nativeLibraryDirectories=[/data/app/com.noodoe.sunray.alpha-1/lib/arm, /data/app/com.noodoe.sunray.alpha-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) at java.lang.ClassLoader.loadClass(ClassLoader.java:511) at java.lang.ClassLoader.loadClass(ClassLoader.java:469) at com.noodoe.sunray.settings.preferences.PreferencesActivity.lambda$setupListener$9(PreferencesActivity.java:248) at com.noodoe.sunray.settings.preferences.PreferencesActivity.access$lambda$9(PreferencesActivity.java) at com.noodoe.sunray.settings.preferences.PreferencesActivity$$Lambda$10.onClick(Unknown Source) at android.view.View.performClick(View.java:5226) at android.view.View$PerformClick.run(View.java:21266) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:168) at android.app.ActivityThread.main(ActivityThread.java:5845) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 代替android.support.v7.widget.SearchView

我通过这样做得到了解决。

答案 6 :(得分:0)

您需要延长SearchView并覆盖dispatchKeyEventPreIme

public class SearchView extends android.support.v7.widget.SearchView {
    public SearchView(Context context) {
        super(context);
    }

    public SearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SearchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean dispatchKeyEventPreIme(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
                onActionViewCollapsed();
            }
        }

        return super.dispatchKeyEventPreIme(event);
    }
}

答案 7 :(得分:0)

点击搜索按钮后,需要为clearFocus()调用SearchView方法。
例如:

mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
    @Override
    public boolean onQueryTextSubmit(String query) {
        mSearchView.clearFocus();
        //...
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }
});

答案 8 :(得分:0)

尝试一下。经过很多尝试,它对我有用。

public static int Visit(int months)
{
    var values = new List<int?> {1, 2, 4, 6, 9, 12, 15, 18, 24, 30, 36, 48, 60};

    return (values.Select((v, i) => new {value = v, index = i})
        .FirstOrDefault(i => months <= i.value)?.index ?? values.Count) + 1;
}

答案 9 :(得分:0)

如果您的SearchView像我一样位于片段上,请在 Kotlin

中使用以下代码段
searchView.setOnQueryTextFocusChangeListener { _, hasFocus ->
      if(!hasFocus){

         // iconify the SearchView when the focus is lost
         searchView.isIconified = true
      }
 }

感谢@vikoo's answer

答案 10 :(得分:-1)

您可以致电view.requestFocus()将焦点放在另一个视图上。这样,搜索视图将以一次背压关闭。