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;
}
});
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) {
}
});
Key presses in software keyboards will generally NOT trigger this method, although some may elect to do so in some situations.
// 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;
}
});