Set Keyboard Mode in android custom keyboard

后端 未结 1 1355
庸人自扰
庸人自扰 2021-02-05 22:38

I have created a Custom Keyboard using the Android Keyboard Class.

I want to have Rows for two modes. One is the normal mode. And one is when the user presses the \"Sym

1条回答
  •  臣服心动
    2021-02-05 23:19

    Ok figured it out myself.

    There is no method to simply switch the keyboard modes. What you have to do is create 2 different keyboards and switch between them manually.

    Here is what the updated XML looks like. NOTE that Rows that you want in both the keywords should not include the flag android:keyboardMode.

    
    
        
        
        
        
        
        
        
        
        
                
    
    
      
    
        
        
        
        
        
        
        
        
        
        
        
    
    
    
    
        
        
        
        
        
        
        
        
        
        
        
    
    

    Then create a integer.xml

    
    
        1
        0
    
    

    Here is the relevant java code. When you create the Keyboard object you pass the keyboard_normal or keyboard_symbol.

     normalKeyBoard = new Keyboard(activity, R.id.board, R.integer.keyboard_normal);
     symbolKeyBoard = new Keyboard(activity, R.id.board, R.integer.keyboard_symbol);
    

    Now create a class variable to keep track of the mode. Default value is R.integer.keyboard_normal

    private int mKeyboardState = R.integer.keyboard_normal;
    

    Now in your onKeyboardActionListner().onKey method put the code to capture the key that switches the modes (assuming that you have created one in your keyboard).

            if( primaryCode== Keyboard.KEYCODE_MODE_CHANGE) {
                if(mKeyboardView != null) {
                    if(mKeyboardState == R.integer.keyboard_normal){
                        //change to symbol keyboard
                        if(symbolKeyBoard== null){
                            symbolKeyBoard = new Keyboard(mHostActivity, R.xml.hexkbd, R.integer.keyboard_symbol);
                        }
    
                        mKeyboardView.setKeyboard(symbolKeyBoard);
                        mKeyboardState = R.integer.keyboard_symbol;
                    } else {
                        if(normalKeyBoard== null){
                            normalKeyBoard = new Keyboard(mHostActivity, R.xml.hexkbd, R.integer.keyboard_normal);
                        }
    
                        mKeyboardView.setKeyboard(normalKeyBoard);
                        mKeyboardState = R.integer.keyboard_normal;
                    }
                    //no shifting
                    mKeyboardView.setShifted(false);
                }
            }
    

    0 讨论(0)
提交回复
热议问题