How to display custom keyboard when clicking on edittext in android

后端 未结 6 575
日久生厌
日久生厌 2020-12-29 13:33

I have a custom keyboard in my application. question is How to didplay this keyboard when click on the edittext.I an using setonfocuschangre listener ,now the custon keyboa

6条回答
  •  我在风中等你
    2020-12-29 14:03

    I created a Custom Keyboard in my application using Keyboard tag. I am adding this keyboard in a RelativeLayout on my screen like.

    private void createCustomKeyboard() {
      Keyboard customKeyboard = new Keyboard(getActivity(), R.layout.keyboard);
      CustomKeyboard mCustomKeyboard = new CustomKeyboard(getActivity(), this);
      mCustomKeyboard.setKeyboard(customKeyboard);
      RelativeLayout relLayKeyboard.addView(mCustomKeyboard);  
    } 
    

    If you want to use this CustomKeyboard on one or more than one EditText then you have to use below code :

    EditText edtxtName = (EditText) v.findViewById(R.id.edtName);
    RelativeLayout relLayKeyboard = (RelativeLayout)findViewById(R.id.relLay_keyboard);
    edtxtName.setOnTouchListener(exitSoftKeyBoard);
    
    private final OnTouchListener exitSoftKeyBoard = new OnTouchListener() {
    
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        InputMethodManager imm = (InputMethodManager) getActivity().getApplicationContext().getSystemService(
                android.content.Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        if(v.equals(edtxtName)){
            edtxtName.requestFocus();
            relLayKeyboard.setVisibility(View.VISIBLE);
        } 
        return true;
      }
    };
    

提交回复
热议问题