How do I determine if the text being edited has focus?

I am connecting to a terminal emulator using a library in android, this connects to a serial device (switch) and shows me the sent / received data. I am sending the connection data through the textbox below the terminal, or by typing in the terminal itself and hitting the enter button on the keyboard in both cases. It will be the only soft keyboard ever used. If I post the wrong string, I am in an unrecoverable state, so I had to provide an implementation for the delete key.

This works fine, if I select terminal I can delete the data. However, if I select editText there is a problem. If I click the Delete button, one character is removed, but the terminal displays two. So if I write "enable" and delete delete, it changes to "enab" on the terminal screen, but what is actually sent is "enabl". So what I need to do is figure out when the editText has focus and if it doesn't fire these lines

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

      

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

The only problem is if I change focus from editText to terminal it sometimes deletes the character, I need it to actually wait for the delete key to be pressed. It doesn't always happen, but it seems like it is stuck in some kind of state where it always thinks the keycode is being deleted and every time I switch between focus the deletion happens. Should I reset the key code after running it or something else? Why does it get stuck thinking the keycode is being deleted? Even after I hit enter and so on. This happens when I hit delete on the editText and it is empty. If editText is empty and there is data in the terminal, it deletes that data correctly, but raises this error. Also, if there is nothing in the editText and nothing in the terminal, nothing is removed, but the error is thrown.

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);
};

      

+3


source to share


3 answers


This little hack seems to work, but I'd love to know why this is happening / the best 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);
};

      

+1


source


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

      



+1


source


First of all, two characters are removed because it dispatchKeyEvent

will be triggered twice, once with KeyEvent.ACTION_DOWN

(on first press) and then again with KeyEvent.ACTION_UP

(on release of the button).

Therefore, you need to check for ACTION_UP

:

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

      

So, this is what is causing your problems. To answer your question in the title, you can check whether an object has EditText

the following form: myEditText.hasFocus()

.

0


source







All Articles