Jayant
Jayant

Reputation: 35

Enter Key Does not Work on Key Listner

I'm using OnKeyListner to Address an 'Enter' key pressed via soft Keyboard on an edit text. [Android.]I want when user press enter key from soft keyboard it should perform some action-->take input from edit box and pass to some function to process. Here's Code:

editbox.setOnKeyListener(new View.OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                        (keyCode == KeyEvent.KEYCODE_ENTER)) {

                    // Code For action on key press

                    input = editbox.getText().toString();
                    if(mEngine.init(input, getApplicationContext()))
                    {                                                          sharePref = getSharedPreferences("info",MODE_PRIVATE);
                        edit = sharePref.edit();
                        edit.putString("editbox",input);
                        edit.commit();


                        //                    inputDialog.dismiss();
                    }
                    else
                    {
                        ............

                    }
                    inputDialog.dismiss();


                    return true;
                }
                return false;
            }
        });     

It seems like not working.Kindly help if i'm doing it wrong.

Upvotes: 0

Views: 157

Answers (1)

Niranj Patel
Niranj Patel

Reputation: 33238

Set this property in your EditText

android:imeOptions="actionDone"
android:imeActionLabel="Enter"

You can also use setOnEditorActionListener Here is sample code..

editbox.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId,
                    KeyEvent event) {
                if(actionId == 0 || actionId== EditorInfo.IME_ACTION_DONE)
                {
                    //Paste your code here.
                }
                return false;
            }

        });

Upvotes: 1

Related Questions