Answer the question
In order to leave comments, you need to log in
The processing of pressing Enter from the keyboard does not work. Only numbers are processed!?
I found several examples about this, somewhere with switch somewhere with if, but that's not the point, it doesn't work anyway. The handler reacts only to pressing numbers. Even default in switch`e does not react. What am I doing wrong? Here is the code
edit.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (KeyEvent.ACTION_DOWN == event.getAction()){
switch (keyCode){
case KeyEvent.KEYCODE_ENTER:
Log.e("TAG", "Нажат интер");
break;
case KeyEvent.KEYCODE_2:
Log.e("TAG", "Нажато 2");
break;
case KeyEvent.KEYCODE_E:
Log.e("TAG", "Нажато e");
break;
default: Log.e("TAG", "Нажато что то");
}
}
return false;
}
});
Answer the question
In order to leave comments, you need to log in
I did that too, it doesn't help. As a result, I did it with the help of TextWatcher. They just said he does not process ENTER, he searched for "\n". Here is the code:
edit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().contains("\n")) {
Log.e("TAG", "Первая строка: Печатается");
} else {
str = s.toString().split("\n");
Log.e("TAG", "Первая строка: " + str[0]);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
You're handling clicks incorrectly. View.OnKeyListener#onKey is called when a button on the physical keyboard is pressed. Reacting to pressing numbers on the software keyboard is probably one of the exceptions mentioned in the documentation:
Handle clicks in EditText like this:
// EditText edit = ..;
edit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN) {
switch(event.getKeyCode()) {
case KeyEvent.KEYCODE_ENTER:
// Нажат Enter
break;
}
}
return false;
}
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question