Paul
Paul

Reputation: 5974

How to tell if an editText is in focus?

I am connecting to a terminal emulator using a library in android, this connects to a serial device (a switch) and shows me sent/received data. I send data over the connection via a text box below the terminal or by typing in the terminal itself and hitting enter on the keyboard in both cases. It will only ever be a soft keyboard that is used. If I send an incorrect string I am in an unrecoverable state so I had to provide a delete key implementation.

This works ok, if I select the terminal I can delete data. However if I select the editText there is a problem. If I press delete then one character is deleted but two appear to be on on the terminal. So if I write "enable" and hit delete it will change to "enab" on the terminal screen but what would actually be sent is "enabl". So what I need to do is figure out when the editText is in focus and if it is do not run these lines

  mSession.appendToEmulator(cmdLeft, 0, cmdLeft.length);
  mSession.appendToEmulator(cmdErase, 0, cmdErase.length);

I've incorporated this: http://developer.android.com/reference/android/view/View.OnFocusChangeListener.html

The only problem is that if I change focus from editText to terminal it deletes a character sometimes, I need it to actually wait for a delete key to be pressed. It doesn't happen all the time, but it seems to get stuck in some state where it always thinks the keycode is delete and every time I switch between focus' a delete occurs. Should I reset the keycode after it is run or something? Why is it getting stuck thinking the keycode is delete? Even after I've pressed enter and so on. It occurs when I press delete in the editText and it is empty. If the editText is empty, and there is data in the terminal it correctly deletes that data but triggers this bug. Also if there is nothing in the editText and nothing in the terminal, nothing is deleted but the bug is triggered.

public boolean dispatchKeyEvent(KeyEvent event) {
    if (event != null && event.getAction() == KeyEvent.ACTION_UP) {

        return false;
    }
if(event.getKeyCode() == KeyEvent.KEYCODE_DEL){

    mEntry.setOnFocusChangeListener(new OnFocusChangeListener(){
        @Override
        public void onFocusChange(View v,boolean hasFocus){

              if (!hasFocus) {
                    mSession.appendToEmulator(cmdLeft, 0, cmdLeft.length);
                    mSession.appendToEmulator(cmdErase, 0, cmdErase.length);
                    Log.d(TAG, "in inner delete");
              }
        }          
    });

    Log.d(TAG, "in delete in delete in delete in delete");
    try {
        sendOverSerial("\b".getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
    return super.dispatchKeyEvent(event);
};

Upvotes: 3

Views: 6029

Answers (3)

Kingsley Mitchell
Kingsley Mitchell

Reputation: 2589

txtEdit.setOnTouchListener(new View.OnTouchListener(){
    public boolean onTouch(View view, MotionEvent motionEvent) {                                                       
         // your code    hope it helps you :)     
         return false;
    }
});

Upvotes: 1

Paul
Paul

Reputation: 5974

This little hack seems to work but I'd really like to know why it is happening/a better solution.

    public boolean dispatchKeyEvent(KeyEvent event) {
    if (event == null || event.getAction() == KeyEvent.ACTION_UP) {

        return false;
    }
   if(event.getKeyCode() == KeyEvent.KEYCODE_DEL){

    mEntry.setOnFocusChangeListener(new OnFocusChangeListener(){

        public void onFocusChange(View v,boolean hasFocus){

              if (!hasFocus && !mEntry.getText().toString().trim().equals("")) {

                    mSession.appendToEmulator(cmdLeft, 0, cmdLeft.length);
                    mSession.appendToEmulator(cmdErase, 0, cmdErase.length);

              }
              else 
              {
                    mEntry.setText(" ");
              }
        }          
    });

    try {
        sendOverSerial("\b".getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

    return super.dispatchKeyEvent(event);
};

Upvotes: 1

Ovidiu Latcu
Ovidiu Latcu

Reputation: 72311

First of all, 2 characters are deleted because the dispatchKeyEvent will be triggered twice, once with KeyEvent.ACTION_DOWN (when it is first pressed), and then again with KeyEvent.ACTION_UP (when the button is released).

So you will need to also perform the check for ACTION_UP :

if( event.getKeyCode() == KeyEvent.KEYCODE_DEL 
    && KeyEvent.getAction()==KeyEvent.ACTION_UP )

So this is what causes your problems. To answer your question in the title, you can check if an EditText has focus like this : myEditText.hasFocus() .

Upvotes: 0

Related Questions