Problems with onKey event

0

I'm servicing an android app made with Eclipse (ADT).

It has a text box, where the product bar code will be typed, and this reading can be done with the reader. When you open Activity, if you enter a value in the text box, the onKey event is fired twice (up and down). If "enter" is entered, a search is performed; the search method focuses on the next box and erases the CB when completed. This works the first time, both typing and using the reader (the reader gives an enter at the end).

The problem is: if I try to read or enter another bar code, the events are not triggered. No logging is generated. It will only be triggered if I press the backspace key.

txtProduto.setOnKeyListener(new OnKeyListener(){

   @Override
   public boolean onKey(View v, int keyCode, KeyEvent event) {
      Log.i("keycode", Integer.toString(keyCode));
      if ((keyCode == KeyEvent.KEYCODE_ENTER)) {
       executarPesquisa();
       return true;
      }

      return false;
    }

 });

There are other events that I do not know what it's for, I do not know if it's affecting. I already commented them and it does not solve.

txtProduto.setOnFocusChangeListener(new OnFocusChangeListener() {

   @Override
   public void onFocusChange(View v, boolean hasFocus) {
    // TODO Auto-generated method stub
    strFocus = "P";
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(txtProduto.getWindowToken(), 0); 

   }
});

This run search invokes the execute method of a class that implements AsyncTask<Void, Integer, Integer>

    
asked by anonymous 29.09.2016 / 16:54

1 answer

1

Takes the event setOnKeyListener and implements the event setOnFocusChangeListener this way:

  txtProduto.setOnFocusChangeListener(new OnFocusChangeListener() {

       @Override
       public void onFocusChange(View v, boolean hasFocus) {
        // TODO Auto-generated method stub
        if (!hasFocus ) {
           if ( !txtProduto.getText().toString().equals("")) {
              strFocus = "P";
              InputMethodManager imm =  (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(txtProduto.getWindowToken(), 0); 

              executarPesquisa();
         }
       }
    });
    
29.09.2016 / 19:25