3

With a Button it is simple,

<Button 
    android:blablabla="blabla"
    ...
    android:onClick="doSomething" />

this will preform the doSomething(View) function.

How can we mimic this with an EditText ? I have read about this and i read that most people use an imeOptions (which still seems necessary) and then implement a actionListener on that EditText object.

This is were i'm lost. Is there a way to implement the "Done"-action (or send or...) from our keyboard to a onClick function like we do with a Button, or do we need to explicitly implement the listener ?

Regards !

MrMe TumbsUp
  • 416
  • 1
  • 4
  • 17

3 Answers3

6

The below code will perform some action when you press the Done key in the softkeyboard.

editText.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
            //do your actions here that you like to perform when done is pressed
            //Its advised to check for empty edit text and other related 
            //conditions before preforming required actions
        }
    return false;
    }
});

Hope it helps !!

codePG
  • 1,754
  • 1
  • 12
  • 19
  • sorry, but this is not what i'm asking. I know how to implement it with listeners. I'm asking if there is a way to add some code in the xml to define what function it should execute after pressing the "done"button. Same way like we implement the android:onClick="dosomething" with our buttons. We don't implement the onClickListener on that button either, we define what function to execute. – MrMe TumbsUp Nov 21 '14 at 14:04
  • @MrMeTumbsUp You should accept this answer, as it is the only way to detect individual button clicks on the IME. – IgorGanapolsky Sep 29 '15 at 01:34
1

I am assuming what you are wanting to do is run some code when the EditText is clicked?

If so, I have found a solution from another thread on the site:

    EditText myEditText = (EditText) findViewById(R.id.myEditText);
    myEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {

    then do this code here

    }
}
});

via: A better way to OnClick for EditText fields?

Community
  • 1
  • 1
Alex
  • 531
  • 1
  • 8
  • 22
  • Nope, i would want to run some code when the user presses "Done" or whatever imeOption you've selected. I know this can be done by Listener, But my question is if it can be done like a Button? just define the name of the function within the xml file. `android:onClick="doSomething"` – MrMe TumbsUp Nov 21 '14 at 01:03
0

Kotlin version

editText.setOnEditorActionListener { view, actionId, event -> 
    if(actionId==EditorInfo.IME_ACTION_DONE){
        // do your actions here that you like to perform when done is pressed
        true // return true from your lambda when the action is treated
    }
    false // return false from your lambda when the action is not treated
}

Java version

editText.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if(actionId==EditorInfo.IME_ACTION_DONE){
            //do your actions here that you like to perform when done is pressed
            return true; // return true when the action is treated
        }
        return false; // return false when the action is not treated
    }
});
Leonardo Sibela
  • 1,613
  • 1
  • 18
  • 39