Tom
Tom

Reputation: 151

How to control which EditText has focus for when performing a barcode scan

I have an activity that contains 3 EditText fields. I originally wrote the code thinking that they must be performed in set order. However I have since learned they can either that user can either scan location Barcode of a product or touch the 2nd edittext and scan a UPC barcode. I am doing a re-write of project that was originally written in VB.net. My struggle is trying to control the focus of which editText has focus for input from scanner. My first version has a public void listener on scanner. I would check if the first edittext has a value and if true then I would know that the scan result goes to 2nd edittext.

I have spent several hours on onTouchListeners with no luck. My question is how can I set which edittext should get result of scan. I know how to ensure that the first field is default but how can I change focus to second editText when that field gets touched. I also don't want default android keyboard to display.

Upvotes: 0

Views: 304

Answers (1)

Raj
Raj

Reputation: 3001

// Set result from barcode in firstEditText

Then -

firstEditText.addTextChangedListener(new TextWatcher(){
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int aft ) {

    }

    @Override
    public void afterTextChanged(Editable s){
        secondEditText.requestFocus();

    }
});

Similarly addTextChangedListener on secondEditText and requestFocus() on thirdEditText.

Upvotes: 1

Related Questions