Android Development: How To Use onKeyUp?

前端 未结 6 1452
执念已碎
执念已碎 2020-12-17 10:22

I\'m new to Android development and I can\'t seem to find a good guide on how to use an onKeyUp listener.

In my app, I have a big EditText,

6条回答
  •  遥遥无期
    2020-12-17 10:51

    If you use device with no touch but with hard keypad buttons (keyboard) use this code to control the events of moving left right up down and ok. use onkeyDown and not onKeyUp because the onKeyup will return the next button events:

            Button myButton = (Button) findViewById(R.id.my_btn);
            myButton .setKeyListener(new KeyListener() {
            @Override
            public int getInputType() {
                return 0;
            }
    
            @Override
            public boolean onKeyDown(View view, Editable editable, int keyCode, KeyEvent keyEvent) {
                Log.d("myTag", "onKeyDown code: " +keyCode);
                switch (keyCode) {
                    case KeyEvent.KEYCODE_DPAD_RIGHT:
                        // USER_MOVE_RIGHT();
                        return true;
                    case KeyEvent.KEYCODE_DPAD_LEFT:
                        //USER_MOVE_LEFT());
                        return true;
                    case KeyEvent.KEYCODE_DPAD_DOWN:
                        //USER_MOVE_DOWN());
                        return true;
                    case KeyEvent.KEYCODE_DPAD_UP:
                        //USER_MOVE_UP();
                        return true;
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                        //USER_Press_OK()
                        return true;
                                   }
                return false;
            }
    

提交回复
热议问题