1

I need a way to take text from a "EditText" as soon as the user presses "Enter" and it should also call a method if it loses focus.

I created EditText like this:

 <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/button2"
        android:ems="10" >
SamSPICA
  • 1,356
  • 14
  • 30
Antilope
  • 291
  • 2
  • 5
  • 16

2 Answers2

1

Use an EditText action listener. Refer to this question, it's probably the same: Handle Enter in EditText

For detecting loss in focus:

if(!(et.isFocused)){
  //call your method here
}
Community
  • 1
  • 1
SamSPICA
  • 1,356
  • 14
  • 30
1

To receive key presses your EditText needs to have focus:

editText1.setFocusableInTouchMode(true);
editText1.requestFocus();

then set the onkeylistener():

editText1.setonKeyListener(new OnKeyListener(

@override onKey(View v, int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_UP && keyCode==KeyEvent.KEYCODE_ENTER){
// what you need to do when Enter is pressed
return true;

}else return false;

}

For focus lost, you need to implement the View.onFocusChange interface and check that hasFocus is false

Mohamed_AbdAllah
  • 5,181
  • 3
  • 26
  • 46