Frage

i want to add onkeylistener to my linear layout but its not working, here is my code. thanks in advance.

 innerlayout.setOnKeyListener(new OnKeyListener() {

                @Override
                public boolean onKey(View v, int arg1, KeyEvent e) {
                    // TODO Auto-generated method stub
                    if(e.getKeyCode()==KeyEvent.ACTION_DOWN){

                        Toast.makeText(HomeScreen.this, "down key is working", Toast.LENGTH_LONG).show();
                        innerlayout.setFocusable(true);
                    }
                    return true;
                }
            });

}
War es hilfreich?

Lösung

As touch events are passed from child to parent. if any child consumes the event (returns true), then it stops; it is not passed to the parent. Are you sure its not consumed elsewhere? Check this, true for one action, false for the rest and super call:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
        //Toast here
            return true;
        }
        **return false;**
    }  

Also, found:
OnKeyListener or OnKeydown not work if children views take the focus
onKeyDown not being called on key press on View
Is this your case? what is the hierarchy of layouts

Andere Tipps

Put

return false;

instead

return true;

Besides using e.getKeyCode() you can directly get the integer value of the action from the int arg1 and also get as e.getAction() and also make sure that you return true as it executes the ACTION_DOWN.

Try out as below:

          @Override
            public boolean onKey(View v, int arg1, KeyEvent e) {
                // TODO Auto-generated method stub
                if(arg1==KeyEvent.ACTION_DOWN){

                    Toast.makeText(HomeScreen.this, "down key is working", Toast.LENGTH_LONG).show();
                    innerlayout.setFocusable(true);
                   return true;
                }
                return false;

EDITED:

setOnKeyListener will only work whenever you will perform some action using the softkeyboard. It will not catch any event of the mouse pointer as you must be trying with the cursor of mouse which will always return the touch event in emulator.

I suggest you to implement the setOnTouchListener for the LinearLayout which will always responds to the touch events on the views

Try out as below its working fine.

      innerlayout.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                Toast.makeText(MainActivity.this, "down key is working",
                        Toast.LENGTH_LONG).show();
                return true;
            } else
                return false;
        }
    });

You almost had it. You just need to set it as focusable and request focus:

innerlayout.setFocusableInTouchMode(true);
innerlayout.requestFocus();
innerlayout.setOnKeyListener(new OnKeyListener() {

            @Override
            public boolean onKey(View v, int arg1, KeyEvent e) {
                // TODO Auto-generated method stub
                if(e.getKeyCode()==KeyEvent.ACTION_DOWN){

                    Toast.makeText(HomeScreen.this, "down key is working", Toast.LENGTH_LONG).show();
                    innerlayout.setFocusable(true);
                }
                return true;
            }
        });

}

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top