EditText、OnKeyListener 或 TextWatcher(条形码扫描)

发布于 2024-10-01 16:06:27 字数 1395 浏览 4 评论 0 原文

我正在使用条形码扫描仪,它将条形码字符串以“12345\n”格式插入到 EditText 中。我不想使用搜索按钮,而是想通过“\n”字符触发搜索事件。我使用了 TextEdit 的 addTextChangedListener 并在该函数中执行了以下操作:

protected TextWatcher readBarcode = new TextWatcher() { 
 @Override
 public void onTextChanged(CharSequence s, int start, int before, int count) {
  // TODO Auto-generated method stub

 }

 @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
  char lastCharacter = s.charAt(s.length() - 1);

  if (lastCharacter == '\n') {
   String barcode = s.subSequence(0, s.length() - 1).toString();
   searchBarcode(barcode);
  }
 }
};

它第一次运行得很好,但我也想在每次扫描后清除 EditText。但不可能在 afterTextChanged 事件中执行此操作,因为它会进入递归循环或其他内容。

这是另一个解决方案,效果很好:

editBarcode.setOnKeyListener(new OnKeyListener() {

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    String barcode = editBarcode.getText().toString();

    if (keyCode == KeyEvent.KEYCODE_ENTER && barcode.length() > 0) {
        editBarcode.setText("");
        searchBarcode(barcode);
        return true;
    }

    return false;
}
});

实际上我不确定什么是正确的方法。也许我可以使用 EditText 的 OnKeyListener 事件。有什么建议吗?

谢谢

I'm using a barcode scanner which inserts barcode string into an EditText in this format "12345\n". Instead of using a search button, I want to trigger the search event by the "\n" character. I used TextEdit's addTextChangedListener and inside that function I'm doing:

protected TextWatcher readBarcode = new TextWatcher() { 
 @Override
 public void onTextChanged(CharSequence s, int start, int before, int count) {
  // TODO Auto-generated method stub

 }

 @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
  char lastCharacter = s.charAt(s.length() - 1);

  if (lastCharacter == '\n') {
   String barcode = s.subSequence(0, s.length() - 1).toString();
   searchBarcode(barcode);
  }
 }
};

It works pretty good for the first time, but I also want to clear the EditText after each scan. But it's not possible to do that inside afterTextChanged event because it's going into a recursive loop or something.

Here is the other solution, which is working pretty good:

editBarcode.setOnKeyListener(new OnKeyListener() {

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
    // TODO Auto-generated method stub
    String barcode = editBarcode.getText().toString();

    if (keyCode == KeyEvent.KEYCODE_ENTER && barcode.length() > 0) {
        editBarcode.setText("");
        searchBarcode(barcode);
        return true;
    }

    return false;
}
});

Actually I'm not sure what is the right way to do it it. Maybe I can use EditText's OnKeyListener event. Any suggestions?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春夜浅 2024-10-08 16:06:27

如果您清除 afterTextChanged() 中的 EditText 内容,则不应出现无限循环。

 @Override 
 public void afterTextChanged(Editable s) { 
  if (s.length > 0) {

      char lastCharacter = s.charAt(s.length() - 1); 

      if (lastCharacter == '\n') { 
       String barcode = s.subSequence(0, s.length() - 1).toString();
       myEditText.setString("");
       searchBarcode(barcode); 
      }
  } 

If you clear the EditText content in the afterTextChanged(), you shouldn't have an infinite loop.

 @Override 
 public void afterTextChanged(Editable s) { 
  if (s.length > 0) {

      char lastCharacter = s.charAt(s.length() - 1); 

      if (lastCharacter == '\n') { 
       String barcode = s.subSequence(0, s.length() - 1).toString();
       myEditText.setString("");
       searchBarcode(barcode); 
      }
  } 
那支青花 2024-10-08 16:06:27

@Hrk 的代码对我不起作用,因为方法 myEditText.setString() 不存在。

这是一种方法:

editText.addTextChangedListener(new TextWatcher() {

  ...

  @Override
  public void afterTextChanged(Editable s) {
    if (s.toString().trim().length() == 0)
      return;

    //do your work, then clear the text here
    s.clear();
  }
});

Code of @Hrk does not work for me, because method myEditText.setString() does not exist.

Here is one way:

editText.addTextChangedListener(new TextWatcher() {

  ...

  @Override
  public void afterTextChanged(Editable s) {
    if (s.toString().trim().length() == 0)
      return;

    //do your work, then clear the text here
    s.clear();
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文