搜索查看imeOptions和onQueryTextSubmit支持 [英] SearchView imeOptions and onQueryTextSubmit support

查看:705
本文介绍了搜索查看imeOptions和onQueryTextSubmit支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用 ActionBarSherlock 4.2 和它的<一个href="https://github.com/JakeWharton/ActionBarSherlock/blob/master/library/src/com/actionbarsherlock/widget/SearchView.java"相对=nofollow>搜索查看小部件在我的应用程序。

I'm currently using ActionBarSherlock 4.2 and it's SearchView widget in my app.

我想让它提交的查询,即使它是空的。我试图设置imeOptions和OnKeyListener但双方没有理由被忽略了。

I wanted to make it submit query even though it's empty. I tried to set imeOptions and OnKeyListener but both were ignored without a reason.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    Log.d(TAG, "onCreateOptionsMenu");
    inflater.inflate(R.menu.interactive_map, menu);
    mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

    mSearchView.setImeOptions(EditorInfo.IME_ACTION_GO);
    mSearchView.setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            Log.d(TAG, "keyCode: " + keyCode); 
            Log.d(TAG, "Action: " + event.getAction());
            if(keyCode == EditorInfo.IME_ACTION_SEARCH){
                onQueryTextSubmit(""+mSearchView.getQuery());
            }

            return false;
        }
    });


    super.onCreateOptionsMenu(menu, inflater);
}

onKey从未被触发的不会出现在窗口的logcat两个logcat的条目。不知道这里面存在是SherlockFragment的问题。

onKey's never get triggered as both Logcat entries never appear in Logcat window. Not sure if this being inside SherlockFragment is the problem.

没什么特别的完成菜单项也是如此。

Nothing special's done to the menu item as well.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/action_search"
          android:title="@string/app_name"
          android:icon="@android:drawable/ic_menu_search"
          android:showAsAction="always"
          android:actionViewClass="com.actionbarsherlock.widget.SearchView" />
</menu>

有没有办法来解决这个问题?还是有更简单的方法,使提交查询时是空的?它似乎并不支持,虽然在源$ C ​​$ C任何配置。

Is there a way to get around this? Or is there simpler way to enable submitting when query is empty? It doesn't seems to support any configuration in the source code though.

推荐答案

我切换通过ActionMode把搜索查看代替。我会接受这作为一个答案,现在,直到有人有这个问题更好的解决方案。

I switched to put SearchView via ActionMode instead. I'll accept this as an answer for now until someone has a better solution for this question.

private void showActionMode(){
    MainActivity a = (MainActivity)getActivity();
    a.startActionMode(new SearchActionMode());
}

private class SearchActionMode implements ActionMode.Callback{

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        Log.d(TAG, "onCreateActionMode creating inflater");
        MenuInflater inflater = new MenuInflater(getActivity());
        Log.d(TAG, "onCreateActionMode inflating");
        inflater.inflate(R.menu.interactive_map, menu);
        Log.d(TAG, "onCreateActionMode finding SearchView");
        mSearchView = (SearchView) menu.findItem(R.id.action_search).getActionView();

        Log.d(TAG, "mSearchView: " + mSearchView);
        setupSearchView();
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        Log.d(TAG, "onPrepareActionMode");
        //TODO add searchView once working
        mSearchView.requestFocus(); 
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
        Log.d(TAG, "onActionItemClicked");
        return false;
    }

    @Override
    public void onDestroyActionMode(ActionMode mode) {
        Log.d(TAG, "onDestroyActionMode");
        closeTopTray();
        closeListView();
    }

}


private void setupSearchView() {

    mSearchView.setIconifiedByDefault(false);
    mSearchView.setId(ID_SEARCH_VIEW);
    mSearchView.setOnSearchClickListener(this);
    mSearchView.setOnQueryTextListener(this);
    mSearchView.setOnCloseListener(this);
    mSearchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);

    // To automatically display keyboard when display SearchView
    mSearchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            Log.d(TAG, "mSearchView focus changed: " + hasFocus);
            if (hasFocus) {
                showInputMethod(v.findFocus());
            }
        }
    });

    mSearchView.setQueryHint("Type keyword");
    mSearchView.setQuery((TextUtils.isEmpty(searchQuery)? "": searchQuery), false);
}

这篇关于搜索查看imeOptions和onQueryTextSubmit支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆