0

I want to detect when the soft key "done" is pressed so that when it is, it a toast message appears. I saw this post about the same question here however, I am confused about how exactly to implement the solution.

The solution provided is:

 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) {
            // toast message
        }
        return false;
    }
 });

my question is this:

  1. is editText suppose to be defined under the onCreate method?
  2. is the entire code suppose to be under the onCreate method?
Community
  • 1
  • 1
thedeepfield
  • 6,138
  • 25
  • 72
  • 107

4 Answers4

1

In short, yes. Unless you only want to activate the EditText under certain conditions then you can put it where the EditTextcomes into play if you'd like (like on click event, in a runnable or after a web call) it comes down to preference.

seaplain
  • 771
  • 7
  • 25
1

You can put your EditText where ever you like. I tend to split everything up into smaller methods,because I think it makes code easier to change, but it is a very common practice to put almost everything in the onCreate method, because it makes code easier to follow when you are not jumping back and forth between different methods.

seaplain
  • 771
  • 7
  • 25
jcw
  • 5,132
  • 7
  • 45
  • 54
0

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

THIS MUST BE CALLED AFTER setContentView

Set in your layout on that edit text ImeOption otherwise you never catch that DONE action.

public ... extends Activity
{

public void onCreate(Bundle ...)
{
super...
setContentVIew...


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) {
            // toast message
        }
        return false;
    }
 });

}
}

<EditText
            android:id="@+id/edittext1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:imeOptions="actionDone"
             >
Mertuarez
  • 901
  • 7
  • 24
0

import :

import android.view.View.OnKeyListener;

try with the following code when on key of perticular edit text.

Code :

ed.setOnKeyListener(new OnKeyListener() {

@Override

public boolean onKey(View v, int keyCode, KeyEvent event) {

            // TODO Auto-generated method stubi

            Log.d("View", "conLL.getWidth() "+conLL.getWidth());

            if (event.getKeyCode()==66 && enteredNumber>0) {

            Log.d("View", "------------------------ ");

             } 
            return false;
        }
});
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
pradeep
  • 185
  • 2
  • 7