EditText OnKeyDown

I declared EditText programmatically (i.e. not in XML) and want to apply the OnKeyDown handler to it. The code shown does not work. Context: I'm trying to grab a short line from the keyboard that should not include control characters (I started with the Enter key). Maybe there is a better way?

Thank!

        public EditText ttsymbol;

/** Called when the activity is first created. */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { 
        switch (keyCode) { 
        case KeyEvent.KEYCODE_ENTER: 
            // IGNOREenter key!! 
            return true; 

        }return false; 
  }
+5
source share
1 answer

You must bind onKeyListener to your text editor.

myEditText.setOnKeyListener(new OnKeyListener() {           
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction()==KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_ENTER) {
                    //do something here
                    return true;
                }
                return false;
            }
        });
+15
source

All Articles