SearchView imeOptions and onQueryTextSubmit support



我目前正在使用ActionBarSherlock 4.2,它是我的应用程序中的SearchView小部件。

我想让它提交查询,即使它是空的。我试图设置imeOptions和OnKeyListener,但两者都被无缘无故地忽略了。

@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窗口中。不确定这是否在夏洛克碎片内部是问题所在。

菜单项也没有什么特别的。

<?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>

有没有办法解决这个问题?或者有更简单的方法可以在查询为空时启用提交?不过,它似乎不支持源代码中的任何配置。

我切换到通过ActionMode放置SearchView。我现在会接受这个作为答案,直到有人对这个问题有更好的解决方案。

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

我遇到了同样的问题,问题出在SearchView中的SubmitQuery()上.java

private void onSubmitQuery() {
    CharSequence query = mQueryTextView.getText();
    if (query != null && TextUtils.getTrimmedLength(query) > 0) {

不支持空查询,所以我不得不下载并使用 ActionBarSherlock,然后修改此方法。

这就是我的onSubmitQuery()现在的样子

private void onSubmitQuery() {
     CharSequence query = mQueryTextView.getText();
     if (query == null) {query = "";}
     if (mOnQueryChangeListener == null
             || !mOnQueryChangeListener.onQueryTextSubmit(query.toString())) {
         if (mSearchable != null) {
             launchQuerySearch(KeyEvent.KEYCODE_UNKNOWN, null, query.toString());
             setImeVisibility(false);
         }
         dismissSuggestions();
     }
 }

希望这有帮助。

相关内容

  • 没有找到相关文章

最新更新