0

I'm looking for code that can catch key press of "DONE" button from soft keyboard. When done button is pressed, I need to change button state to enabled and user then can move on to the next activity.

I found this piece of code, here on stackoverflow, but I can't implement it without errors. Can you help me please?

editText = (EditText) findViewById(R.id.edit_text);

editText.setOnEditorActionListener(new OnEditorActionListener() {
   @Override
   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
       if (actionId == EditorInfo.IME_ACTION_DONE) {
           // do your stuff here
       }
       return false;
}
});

Here is entire .java file

package com.example.start201;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class FirstActivity extends Activity {

   private EditText editText;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.layout_firstactivity);

       editText = (EditText) findViewById(R.id.editText2);

       editText.setOnEditorActionListener(new OnEditorActionListener() {
           @Override
           public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
               if (actionId == EditorInfo.IME_ACTION_DONE) {
                   // do your stuff here
               }
               return false;
           }
       });

}


}
Samuel Edwin Ward
  • 6,526
  • 3
  • 34
  • 62
Armand
  • 2,611
  • 2
  • 24
  • 39
  • What errors do you have? – ObAt Jan 25 '13 at 15:00
  • Do you think that the errors are just random and useless? Why not tell us what errors you get? – Simon Jan 25 '13 at 15:00
  • I get these errors: Multiple markers at this line - The method setOnEditorActionListener(TextView.OnEditorActionListener) in the type TextView is not applicable for the arguments (new OnEditorActionListener(){}) - OnEditorActionListener cannot be resolved to a type – Armand Jan 25 '13 at 15:02
  • Did you import `OnEditorActionListener`? Also see http://stackoverflow.com/questions/2004344/android-edittext-imeoptions-done-track-finish-typing and http://stackoverflow.com/questions/5077425/android-detect-done-key-press-for-onscreen-keyboard – ObAt Jan 25 '13 at 15:07
  • define "errors". fix your imports. – njzk2 Jan 25 '13 at 15:12
  • Thanks for the clues guys. Now I get it and it's working fine. Next time I'll define my questions more precisely. – Armand Jan 25 '13 at 16:13

2 Answers2

12

I got it sorted, this code runs smoothly and it recognizes key press of DONE button from soft keyboard.

Layout file:

   <EditText
    android:id="@+id/editText1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:imeOptions="actionDone"
    android:inputType="numberDecimal" />

   <Button
    android:id="@+id/button1"
    android:layout_width="80dp"
    android:layout_height="30dp"
    android:text="NEXT"
    android:textSize="10sp" />

</RelativeLayout>

.java file:

package com.example.drywallcalculator102v;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class FirstActivity extends Activity {

   private EditText editText;
   private Button btnNext;

   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.layout_firstactivity);

       btnNext = (Button) findViewById(R.id.button1);
       btnNext.setEnabled(false);

       editText = (EditText) findViewById(R.id.editText2);

       editText.setOnEditorActionListener(new OnEditorActionListener() {

           @Override
           public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
               // TODO Auto-generated method stub
               if (actionId == EditorInfo.IME_ACTION_DONE) {
                   // do your stuff here
                   btnNext.setEnabled(true);
               }
               return false;
           }
       });

   }


}
Armand
  • 2,611
  • 2
  • 24
  • 39
-1

try adding this import:

import android.widget.TextView.OnEditorActionListener;
Spinettaro
  • 300
  • 2
  • 7