...">

Android EditText imeOption OnClick

With a button it's easy,

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

      

this will remember the doSomething (View) function.

How can we simulate this with EditText? I read about this and I read that most people use imeOptions (which still seems to be necessary) and then implement an actionListener for that EditText object.

It was me who was lost. Is there a way to implement the "Done" action (or send or ...) from our keyboard to the onClick function like we do with the button, or do we need to explicitly implement the listener?

Respectfully!

+3


source to share


2 answers


The code below will do some things when you press a key Done

on your keyboard.

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!

+3


source


I guess what you want to do is run some code when you click on the EditText?

If so, I 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: Best way to OnClick for EditText fields?

+1


source







All Articles