Problems with onKey event

Asked

Viewed 71 times

0

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

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

The problem is: if I try to read or enter another barcode, the events are not triggered. No log is generated. It will only be fired 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 some other events that I don’t know what it’s for, I don’t know if it’s affecting. I’ve commented on them and it doesn’t 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 query calls the method executeof a class that implodes AsyncTask<Void, Integer, Integer>

  • Take the event keylistener and treat in the event focuschange this out of question?

  • It will not. Once the barcode is read, you need to fire the search. I need you to identify enter to perform the search. The player "gives" the enter. The way it works only once.

  • 1

    If the reader gives the enter, the setOnFocusChangeListener event occurs. In this event you can call the query. It would not be feasible?

  • I’ll take a look and tell you

1 answer

1

Strip the event setOnKeyListener and implements the event setOnFocusChangeListener in that 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();
         }
       }
    });
  • Now after the reader enters the numbers, he’s breaking a line on Edit. I put android:maxLines="1" but still breaks a line and does not lose focus

  • 1

    See the reader setting. It must be inserting something other than <enter>

  • I decided putting android:singleLine="true" , I returned the event onKey, but I did an if to see if the event was up. It is working so for now. Thanks for the help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.