Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Android, error when checking if editText is in focus.

Options
  • 24-01-2013 3:21pm
    #1
    Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,087 Mod ✭✭✭✭


    I wrote a terminal using a library. If I select the editText under the terminal in my app there is a problem. If I press delete then one character is deleted but two appear to be deleted 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 when I press enter 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 from my method:
      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

    It works ok, 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. The program will work fine for a little bit before this occurs. I think 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);
    };
    


Comments

  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,087 Mod ✭✭✭✭Tar.Aldarion


    This little hack seems to work but I'd really like to know why it is happening/a better solution. I would consider what I did pretty bad programming/bad 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){
                      
                   [B]   if (!hasFocus && !mEntry.getText().toString().trim().equals("")) {[/B]
        
                    	    mSession.appendToEmulator(cmdLeft, 0, cmdLeft.length);
                    	    mSession.appendToEmulator(cmdErase, 0, cmdErase.length);
                    	   
                      }
                    [B]  else 
                      {
                            mEntry.setText(" ");
                      }[/B]
                }          
            });
        
        	try {
        		sendOverSerial("\b".getBytes("UTF-8"));
        	} catch (UnsupportedEncodingException e) {
        		e.printStackTrace();
        	}
        }
        
            return super.dispatchKeyEvent(event);
        };
    


Advertisement