how to set a timer or delay on some function

I have a Bluetooth bcr hooked up to my tablet. I want it to clear the textbox if it doesn't start with a R. The problem is that there is Inter-Character delay(around 5ms) on it to prevent data loss during the transmission so it had already cleared out the EditText before is done typing. So the barcode that is called KM70083 looks like M70083 in the EditText, so it actually only deletes the first character instead of clearing the whole EditText

public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    final String text = shelfnumberbox.getText().toString().trim();
    if (text.matches("K")) {
        Toast.makeText(getApplicationContext(), "First charater has to be R", Toast.LENGTH_SHORT).show();
        shelfnumberbox.setText("");
        shelfnumberbox.requestFocus();
        return;
    }
}

Upvotes: 0

Views: 46

Answers (2)

Chris Stillwell
Chris Stillwell

Reputation: 10547

Change your if statement to be if(!text.startsWith("R")). That way you will clear out the text regardless of how many characters are in the string.

Upvotes: 2

user3765370
user3765370

Reputation: 130

Use TextWatcher afterTextChanged() method to do your work. You can use a timer and schedule your cleaning task there.

Upvotes: 0

Related Questions