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



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

我已经为indic unicode语言设计了自定义键盘,它可以很好地与EditText视图配合使用。

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

提前谢谢。

如果您一直在学习本教程,那么需要进行一些调整才能在操作栏中使用SearchView而不是EditText。

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

寄存器编辑文本,现在寄存器搜索查看

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

onKey

@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

相关内容

  • 没有找到相关文章

最新更新