EditText onEditorAction

3

I'm making an application where I enter the user code by means of a manual scanner, I need to trigger an event just when the EditText receives the barcode data.

Deactivate the option to receive data through the keyboard so it is not displayed.

This is my java code.

edtEmployeeID.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    Toast.makeText(getApplicationContext(), edtEmployeeID.getText(), Toast.LENGTH_SHORT).show();
                    return true;
                }
                return false;
            }
        });

Although I have not entered it using the scanner.

In xml

<EditText
            android:id="@+id/edtEmployeeID"
            android:layout_width="250dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:singleLine="true"
            android:hint="@string/code"
            android:textColorHint="@color/colorSecundaryText"
            android:textSize="25sp"
            android:textAlignment="center"
            android:textColor="@color/colorPrimaryText"
            android:cursorVisible="false"
            android:focusable="true"/>
    
asked by Max Sandoval 11.04.2016 в 19:04
source

1 answer

2

Well, at the end I opted to change the method and use addTextChangedListener() to detect a change within EditText , and it worked just as I wanted.

 edtEmployeeID.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
                if (s.toString().contains("\n")){
                    Toast.makeText(getApplicationContext(), edtEmployeeID.getText(), Toast.LENGTH_SHORT).show();
                    edtEmployeeID.setText("");
                }

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });
    
answered by 11.04.2016 / 20:05
source