-1

I am trying to create commands for editText, so when you type in a keyword some code will run. I am using this code now, but it doesn't seem to work.

    @Override
    public void editText (String editText)
    {
        if(editText == ("closer"))
        {
            ((ViewGroup.MarginLayoutParams) red.getLayoutParams()).topMargin += 1;
//                image.setRotation(image.getRotation() + 1);
            red.requestLayout();
            ((ViewGroup.MarginLayoutParams) blue.getLayoutParams()).topMargin -= 1;
//                image2.setRotation(image2.getRotation() + 1);

        }

When i type in the word and hit enter, the edit text just creates a new line. But I want to be able to hit enter and the code runs, on what ever you type in.

3 Answers3

1

You can handle the enter key like this:

editText.setOnKeyListener(new OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        // If the event is a key-down event on the "enter" button
        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER)) {
            // Perform action on key press
            if(editText.getText().equals("closer")){
                ((ViewGroup.MarginLayoutParams)red.getLayoutParams())
                .topMargin += 1;
                red.requestLayout();
                ((ViewGroup.MarginLayoutParams)blue.getLayoutParams())
                .topMargin -= 1;
            }
            return true;
        }
        return false;
    }
});
rpurohit
  • 364
  • 1
  • 10
0

Attach TextWatcher to your EditText to catch the user input. http://developer.android.com/reference/android/text/TextWatcher.html

editText.addTextChangedListener(new TextWatcher() {  
 ...
});
Eugene Krivenja
  • 647
  • 8
  • 18
0

Try setting singleline attribute of your EditText to true

android:singleLine="true"
Collins Abitekaniza
  • 4,496
  • 2
  • 28
  • 43