2016-07-14 68 views
0

我有一个蓝牙bcr连​​接到我的平板电脑。我想让它清除文本框,如果它不以R开头。问题在于它存在字符间延迟(约5ms),以防止在传输过程中丢失数据,因此在键入之前它已经清除了EditText。所以被称为KM70083条形码看起来像在的EditText M70083,所以它实际上只删除了第一个字符,而不是清除整个的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; 
    } 
} 
+1

你可以用'editText.postDelayed(新的Runnable(){@Override 公共 无效的run(){ }} ,500);' – Vucko

+0

另外你的代码使用火柴,这是一个正则表达式的表达,也许你应该考虑每次只看第一个字符而不是正则表达式。 – JoxTraex

回答

2

将您的if声明更改为if(!text.startsWith("R"))。这样,无论字符串中有多少个字符,都将清除文本。

+0

然后我得到#21:错误膨胀类 在Toast.makeText(getApplicationContext(),“第一个字符必须是R”,Toast.LENGTH_SHORT).show(); shelfnumberbox.setText(“”); –

+0

问题是(!!删除后,我工作正常,但我仍然不得不使用shelfnumberbox.postDelayed(新Runnable(){@Override公共无效运行(){ shelfnumberbox.setText(“”); shelfnumberbox。 requestFocus(); }},1250); Vucko帮助 得到它的工作probaly 但我也认为,这一个更好,因为我认为它也将清除文本,如果架contians一个R文字中间的字母 –

0

使用TextWatcher afterTextChanged()方法来做你的工作。您可以使用计时器并安排您的清洁任务。