gpt4 book ai didi

android - 需要帮助让 Android TextWatcher 处理传入的复合条形码数据

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:41:59 24 4
gpt4 key购买 nike

我正在编写一个使用外部连接的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com