Android:如何在SearchView中显示自定义键盘?

时间:2014-11-08 16:49:47

标签: android android-layout keyboard searchview android-keypad

如何在Android应用程序的SearchView中显示自定义键盘视图?

我为indic unicode语言设计了自定义键盘,可以正常使用EditText视图。

我想用SearchView附加键盘,请建议隐藏默认键盘并显示自定义键盘的选项。

提前致谢。

1 个答案:

答案 0 :(得分:0)

如果您一直关注this tutorial,则需要进行一些调整才能在操作栏中使用SearchView而不是EditText。

更改的方法是registerEditText和OnKeyBoardActionListener中的onKey:

registerEditText ,现在是registerSearchView

    public void registerSearchView(SearchView searchView, final MenuItem searchMenuItem){
    SearchManager searchManager = (SearchManager) mHostActivity.getSystemService(Context.SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(mHostActivity.getComponentName()));
    this.searchMenuItem = searchMenuItem;
    final IBinder searchToken = searchView.getWindowToken();
    searchView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            Log.d("Focus change", "hasFocus: " + hasFocus);

            if (hasFocus) {
                showCustomKeyboard(v);
            }
            else
                hideCustomKeyboard();

        }
    });

    //The searchview clicked after the it is already in focus
    searchView.setOnClickListener(onClickListener);

    //When the searc view is clicked before it is focused
    searchView.setOnSearchClickListener(onClickListener);

    searchView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d("Touch", event.getAction() + "");
            SearchView searchViewTemp = (SearchView) v;
            int inType = searchViewTemp.getInputType();         //backup the input type
            searchViewTemp.setInputType(InputType.TYPE_NULL);   //Disable standard keyboard
            searchViewTemp.onTouchEvent(event);
            searchViewTemp.setInputType(inType);
            return true;
        }
    });

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            searchMenuItem.collapseActionView(); //collapsing the searchview after search
            return false;
        }

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

    //Disable spell check
    searchView.setInputType(searchView.getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
}

private View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d("onClick", v.getLayerType()+"");
        showCustomKeyboard(v);
        InputMethodManager imm = (InputMethodManager) mHostActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
};

<强>安其

@Override
    public void onKey(int primaryCode, int[] keyCodes) {
        View focusCurrent = mHostActivity.getWindow().getCurrentFocus();
        Class currentClass = focusCurrent.getClass();
        Class searchClass = mHostActivity.findViewById(R.id.action_line_search).getClass();
        SearchView.SearchAutoComplete sac = new SearchView.SearchAutoComplete(mHostActivity);
        Class searchAutoCompleteClass = sac.getClass();
        EditText editText = (EditText)mHostActivity.findViewById(R.id.edittext_search_lines);
        Class editTextClass = editText.getClass();

        if( focusCurrent.getClass() == SearchView.SearchAutoComplete.class) {

            SearchAutoComplete searchView = (SearchAutoComplete) focusCurrent;
            Editable editable = searchView.getText();
            int start = searchView.getSelectionStart();
            if (primaryCode == Keyboard.KEYCODE_DONE) {
                Log.d("onKey", "Search Clicked");
                mHostActivity.sendQuery(editable);
                hideCustomKeyboard();
            } else if (primaryCode == Keyboard.KEYCODE_DELETE) {
                Log.d("onKey", "Deleting input");
                if (editable != null && start > 0)
                    editable.delete(start - 1, start);
            } else {
                Log.d("onKey", "Letter or number clicked");
                editable.insert(start, Character.toString((char) primaryCode));
            }
        }
    }

然后,当您想要注册SearchView时,您在活动或片段中使用新的registerSearchView。

但是出现了一个新问题:默认软键盘出现在自定义键盘的顶部。如果你弄清楚如何删除它,请告诉我! :d