- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在编写一个使用外部连接的 USB 条形码/RFID 扫描仪的应用程序。正在扫描的数据是“复合”数据。这是一个例子:
=+03000=W12560712345600=%2800&>0090452359
这是来自复合数据扫描。数据中的分隔符是等号 (=) 或与号 (&)。第一位=+03000
表示扫描中有三个数据部分:
=W12560712345600
=%2800
&>0090452359
此数据可以有从 1 到 N 的任意数量的数据部分。
在我的 Android 应用程序中,我有一个包含三个 EditText
元素的表单。对于这个复合扫描数据,我需要做的是使用分隔符将其分解,并将每条数据粘贴到正确的 EditText
字段中。
在经历了很多痛苦之后,我意识到我不能只操纵 stdin,而且我需要在我的 EditText
之一上使用 TextWatcher
捕获扫描数据的字段,以便我可以对其进行操作。
我的问题是我不知道该怎么做。这是我所拥有的:
activity_main.xml
<LinearLayout>
<TextView />
<EditText android:id="@+id/datafield01" />
<EditText android:id="@+id/datafield02" />
<EditText android:id="@+id/datafield03" />
<Button />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText dataField01 = (EditText)findViewById(R.id.datafield01);
EditText dataField02 = (EditText)findViewById(R.id.datafield02);
EditText dataField02 = (EditText)findViewById(R.id.datafield03);
dataField01.addTextChangedListener(editTextWatcher);
}
TextWatcher editTextWatcher = new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
}
@Override
public void afterTextChanged(CharSequence s){
}
};
}
我已经尝试将 CharSequence
捕获到 StringBuffer
- 使用 before、on 和 afterTextChanged - 这样我就可以对其进行操作并将其放入正确的 EditText
元素,但是我一直没有成功。
修改后的 MainActivity.java
public class MainActivity extends AppCompatActivity {
private StringBuffer stringBuffer;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
stringBuffer = new StringBuffer();
EditText dataField01 = (EditText)findViewById(R.id.datafield01);
EditText dataField02 = (EditText)findViewById(R.id.datafield02);
EditText dataField02 = (EditText)findViewById(R.id.datafield03);
dataField01.addTextChangedListener(editTextWatcher);
System.out.println(stringBuffer.toString());
}
TextWatcher editTextWatcher = new TextWatcher(){
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
stringBuffer.append(s);
}
@Override
public void afterTextChanged(CharSequence s){
}
};
}
每当我尝试 System.out.println(stringbuffer);
时,那里什么也没有。
扫描的代码没有给出换行符或任何其他类型的分隔符表示它已经结束,数据部分的数量可以是一到 N,但每个数据部分确实有一个已知的固定长度。
一定有其他人在做这样的事情,但我的谷歌搜索没有结果。
那么,有什么方法可以完成我想做的事情,还是我完全不走运?
谢谢。
最佳答案
我确信有一种方法可以通过一些 TextWatcher
实现来做到这一点;当您看到分隔符 char 等时切换焦点。
但我认为当您知道条形码扫描器的所有输入时,处理起来会更容易。然后您可以简单地解析数据并将其放在适当的位置。
所以诀窍是弄清楚您何时拥有所有扫描仪输入,以便您可以解析它。
下面是一些代码,使用了我从查看 Android 适配器的 Filter
类中学到的技巧。有一个延迟机制可以对多次快速按键仅进行一次过滤。 (遗憾的是,它是 Google 私有(private)的,所以我们凡人不应该使用这个有用的功能。)
由于您的扫描仪显然像键盘一样工作,因此按键暂停应该意味着扫描仪已完成输入。让我们用它来确定扫描何时完成。
首先,我们将定义一个回调接口(interface),因为我是一个 IoC 类型的人:
interface ScanProcessor {
void process(CharSequence input);
}
现在我们将定义一个Handler
类。此处理程序将在主 (UI) 线程上运行,因此我们不能在处理器中太忙。
public static class InputDelayHandler extends Handler {
private static final int INPUT_MESSAGE = 0x20170308;
private static final int PROCESS_MESSAGE = 0x20170309;
// You'll have to tweak this value to ensure all input is entered,
// while the app remains responsive to the scan
private static final long DELAY_MS = 250;
private CharSequence mInput;
private ScanProcessor mScanProcessor;
InputDelayHandler(ScanProcessor scanProcessor) {
super(); // associate the handler with the Looper for the current thread
mScanProcessor = scanProcessor;
}
void setInput(CharSequence input) {
// removing this message is the key to how this works
// since it's delayed, we can cancel before input is complete.
removeMessages(PROCESS_MESSAGE);
removeMessages(INPUT_MESSAGE);
Message inputMessage = obtainMessage(INPUT_MESSAGE);
inputMessage.obj = input;
sendMessage(inputMessage);
Message startMessage = obtainMessage(PROCESS_MESSAGE);
sendMessageDelayed(startMessage, DELAY_MS);
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case INPUT_MESSAGE:
// remember the last input
mInput = (CharSequence) msg.obj;
break;
case PROCESS_MESSAGE:
// callback
if (/*mInput looks like valid scan data*/) {
mScanProcessor.process(mInput);
}
break;
}
}
}
具有 EditText
字段的类应该保留对处理程序的引用。
private InputDelayHandler mHandler;
现在您的 TextWatcher
只需告诉处理程序发生了一些事情:
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mHandler.setInput(s);
}
这将它们连接在一起:
mHandler = new InputDelayHandler(new ScanProcessor() {
@Override void process(CharSequence input) {
// parse the input, set the EditText fields, etc.
}
});
关于android - 需要帮助让 Android TextWatcher 处理传入的复合条形码数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42565479/
我有一个正在动态生成的条形码,并希望传递给 gsp,该 gsp 稍后将变成 pdf。我不需要出于自己的任何目的保留条形码,只想将图像从 Controller 传递到 gsp。 有没有办法渲染作为变量传
我正在研究苹果“passkit”框架。我已经制作了 lolipop 优惠券,正如它在 guide 中所说的那样. - 创建证书(Apple Dev 网站)我已经编译了.pkpass。 现在我必须使用动
我有这样的代码 $('.testbarcode').barcode('1234567', "code128"); 当我设置样式时它是折线,那么我如何才能将此条码的宽度设置得
我正在将 Visual Basic 应用程序转换为 Python Django。目前,它具有条形码功能来处理商店的销售。这可以用 python django 实现吗? 最佳答案 如果您对条形码功能的定
我正在尝试按照此示例使用条形码 API https://github.com/googlesamples/android-vision/tree/master/visionSamples/barcod
这是我到目前为止所得到的: def encodeFive(zip): zero = "||:::" one = ":::||" two = "::|:|" t
我正在尝试创建具有本文所示格式的条形码:http://www.thebookdesigner.com/2009/10/self-publishing-basics-deciphering-the-bo
我需要一些关于如何从 bmp 文件中获取 12 位条形码的指导,我完全不知道如何处理这个问题。我首先将图像读入 bitmam,我该如何继续? 例子:下图的条码是081034489030。我如何获得这些
谁能告诉我从哪里开始编码以便从 C# 中的 OPOS(Datalogic Magellan 设备)加权和条形码扫描中获取数据?例如,在这种情况下我应该使用什么库和什么函数。我一无所知,因为我已经花了很
使用 zxing,我设法将 Code39 条形码保存为 PNG,但它只显示条形,没有数字。我怎样才能在一个 PNG 中包含条形码和编号? KI 最佳答案 您无法使用 zxing 添加号码。您可以做一些
我正在尝试将扫描的条形码用作 SQL 查询中的变量。我正在使用 Google ML Kit Quick Start项目。我有一个使用 jtds1.3.1 制作的连接和查询功能。我只是无法获得要在查询中
我尝试让程序从下面的代码中读取 EAN13,但它不起作用 func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metada
我必须读取 PDF 文件中的数据矩阵代码,我正在研究其可能性,对此我有一些疑问: 1.- 我与 Itext 合作,我正在寻找有关使用此库读取此 QR 条形码的可能性的信息,但我没有相关结果,这可能吗?
文档和头文件不包含任何与支持 3D 条形码相关的信息。但是,明确提到 AVMetadataMachineReadableCodeObject 支持一维和二维条码。因此,有人知道 AVFoundatio
我一直在寻找解决方案,到目前为止,我认为对我的项目来说最好的解决方案是使用免费的 Code 39 字体。但是,我已经尝试打印一些样本,但我的条形码扫描仪无法读取它们。 我进行了更多研究,偶然发现了 t
生成 3 of 9 很容易条形码使用 Font() Font f = new Font("Free 3 of 9", 80); this.Font = f; Label l = new Label()
愚蠢的我,我以为只要用条形码字体写一些文本,就会让扫描仪读取它。看来我错了。 所以在阅读了一些关于 code128 条形码的文档之后,我了解到: 条形码以(103、104 或 105 取决于类型)开头
我想在 WPF 应用程序中区分(条形码)扫描仪和键盘输入。 我需要的是每当我的扫描仪提供数据时发生的事件。 在我的应用程序中有一个特殊的字段,它将填充来自扫描仪的输入。因此,如果用户关注其他领域,我不
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
是否可以将 ZPL 和二进制数据用于 aztec 条形码? 我尝试使用BluetoothConnection写入发送以UTF8和字节数据编码的字符串连接数组/ String zplStart; byt
我是一名优秀的程序员,十分优秀!