onKeyListener not working with soft keyboard (Android)

前端 未结 6 629
太阳男子
太阳男子 2020-11-30 07:07

I am using onKeyListener to get the onKey events. It works fine with the normal keyboard. But it does not work with soft keyboard. I am only able to get onKey events for num

6条回答
  •  南笙
    南笙 (楼主)
    2020-11-30 07:29

    setFocusableInTouchMode(true); //Enable soft keyboard on touch for target view
    
    setFocusable(true); //Enable hard keyboard to target view
    

    example:

    public class CanvasView extends View{
        public CanvasView(Context c){
            super(c);
    
            //enable keyboard
            setOnKeyListener(new KeyBoard());
            setFocusable(true);
            setFocusableInTouchMode(true);
        }
    } 
    

提交回复
热议问题