i would like to know if its possible to detect whenever a user presses the back button when in the EditText field. I've tried using the KeyEvent.BACK and the KeyEvent.CANCELED flags in my setOnKeyListener like so:
textView.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN || keyCode == KeyEvent.FLAG_CANCELED || keyCode == KeyEvent.BACK) {
Log.e("TextView", String.valueOf(thiz.getText()));
}
return false;
}
});
but this didn't work. If somebody could help me i would appreciate that :)
As per my understanding if you want to do something("I would like to log the text of the edittext whenever an user closes the keyboard") when keyboard is hidden, you can use ViewTreeObserver like this example
As I understand that you want, for some reason, to log text from EditText when the user closes the virtual keyboard. If yes, check this question, possibly this is what you searching for. Especially you may take look at this answer
Related
mainNotes.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if ((keyCode != KeyEvent.KEYCODE_SLASH) && (titleMod = true) && (keyCode != 46) && (keyCode != KeyEvent.KEYCODE_SLASH) && (keyCode != KeyEvent.KEYCODE_ENTER)) {
Toast.makeText(MainActivity.this, "not a slash", Toast.LENGTH_SHORT).show();
titleEnd += 1;
slashCount = 1;
} else if ((keyCode == KeyEvent.KEYCODE_DEL) && (titleMod = true)) {
Toast.makeText(MainActivity.this, "deleted", Toast.LENGTH_SHORT).show();
titleEnd -= 1;}
The code does not recognize that delete is pressed. It works for all other keypresses like slash, other text and so on. It doesn't even register that delete is pressed?
KeyEvent.KEYCODE_DEL corresponds to your backspace key. Standard Android keyboards don't actually have a delete key at all, so you need to be pressing backspace when testing on emulator via keyboard.
To make sure that soft key events are detected, avoid using the KeyListener interface because it's unlikely to be recognized by a software keyboard or emulator. From the official documentation:
Key presses on soft input methods are not required to trigger the
methods in this listener, and are in fact discouraged to do so. The
default android keyboard will not trigger these for any key to any
application targetting Jelly Bean or later, and will only deliver it
for some key presses to applications targetting Ice Cream Sandwich or
earlier.
Instead, try using KeyboardView.OnKeyboardActionListener.
Sample code may be found here.
i am creating an android game and have an onTouchEvent like this:
#Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (!action) {
action = true;
}
} else {
action=false;
}
return true;
}
My problen is now, that action is false as soon as i swipe my finger a little bit.
thanks for helping,
Florian
If you want to return true even when a swipe input occurs, you could store the last action to compare if the last action was ACTION_DOWN and if the current 'event.getAction()' is also ACTION_DOWN, then you can assume that the swipe action occurs (also if the previous 'x' and 'y' positions differ to the current 'x' and 'y' positions), therefore still return true. Any other scenario, you can return false.
You could also try looking in the android API documentation about a swipe input action/gesture.
Hope that is helpful.
I'm currently working on a practice program that works with various keyboard inputs so I can understand how they work in Android apps with Java. But I am struggling to get the ESC key to be recognised. The code below doesn't work. Has anyone ever managed to get the ESC key to work in an app? Is it actually possible?
editText.setOnEditorActionListener(new OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
boolean handled = false;
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_ESCAPE) {
textView.setText("Escapekey pressed");
handled = true;
}
return handled;
}
});
To summarize, I want the program to recognise the ESC key.
It seems that Android Emulator will not forward this key to the application, just like e.g. the function keys. Receiving these key events on a real device from a real keyboard works fine for me.
How can i skip a keyevent if the typed key is a specific key ( in my case enter) ? When i press enter i want to click on the button and stop there, without continuing the code that comes next in this method. Seems like the "consume()" doesn't work.
public void keyPressed(KeyEvent arg0) {
if(arg0.getKeyCode() == KeyEvent.VK_ENTER && button.isDisplayable()){
button.doClick();
arg0.consume();
}}
Just stick a return statement underneath.
arg0.consume();
return;
Read the documentation on the KeyEvent. KeyEvent.consume() doesn't prevent the method from continuing to execute, it prevents any other listener method from executing due to the event.
Arrange the logic of your method so that no other code executes if the key pressed was a return key.
i have a button that call a method, in this method it call another method to connect to the DB and return results, if results positive, change the labels and make a button ENABLED, and if the results is negative, the Button still disabled
the problem is, i have set in the TF a keytyped event, if someone type something new in it, disable the btnEditar:
public void keyTyped(KeyEvent e) {
btnEditar.setEnabled(false);
btnDeletar.setEnabled(false);
}
i dont want this event "capture" the enter to disable the button
there is a way to do that or i have to think i another logic way?
As others have pointed out, there are other ways to do this besides using a KeyListener. I will respond to your original attempt below. A KeyListener is a functional and easy tool to use for this job.
Use keyPressed instead of keyTyped, and then you'll have a valid key code that you can use to ignore enter presses:
public void keyPressed(KeyEvent e) { // not keyTyped!
if (e.getKeyCode() != KeyEvent.VK_ENTER) {
btnEditar.setEnabled(false);
btnDeletar.setEnabled(false);
}
}
If you insist on using keyTyped for some reason, you won't have a key code available, but you can cover most cases by checking the character for a newline or carriage return:
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() != 13 && e.getKeyChar() != 10) {
btnEditar.setEnabled(false);
btnDeletar.setEnabled(false);
}
}
Use a DocumentListener to listen for changes to the text in the Document. Read the section from the Swing tutorial on How to Write a Document Listener.