简体   繁体   English

扫描时删除条形码字符

[英]Drop barcode character on scanning

I have android an application with a bar-code scanner in the main activity. 我在主要活动中有一个带有条形码扫描仪的android应用程序。 After scanning the bar-code I search for this number in a database. 扫描条形码后,我在数据库中搜索此号码。

My problem is that when I scan the bar-code, not all characters are read. 我的问题是,当我扫描条形码时,不会读取所有字符。 For example, if I try to scan 807251306246 , after scanning this bar-code, one or two characters are randomly removed. 例如,如果我尝试扫描807251306246 ,则在扫描此条形码后,将随机删除一个或两个字符。

I tried to create a new application that reads the bar-code and views it in a list. 我试图创建一个新的应用程序,该应用程序读取条形码并在列表中查看它。 It works fine. 工作正常。 I use the same code in both apps. 我在两个应用程序中使用相同的代码。

The code: 编码:

public class MainActivity extends AppCompatActivity {

EditText et;
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et=(EditText) findViewById(R.id.et);
    tv=(TextView) findViewById(R.id.tv);

    et.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View view, int i, KeyEvent keyEvent) {
            if (i == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == keyEvent.ACTION_DOWN) {
                barcodeInserted();
                return true;
            } else if (i== KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == keyEvent.ACTION_UP)
                return true;
            return false;
        }
    });
}
public void barcodeInserted() {
   tv.append(et.getText().toString()+"\n");
}

XML file: XML档案:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.test.barcode.barcodescanner.MainActivity">
<EditText
    android:id="@+id/et"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:singleLine="false"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_bias="0.0" />

<TextView
    android:id="@+id/tv"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/et" />

</android.support.constraint.ConstraintLayout>

This code accepts input into an edittext and waits for the end of the input 此代码接受输入到edittext中,并等待输入结束

etInput.requestFocus();
        etInput.setOnEditorActionListener((v, actionId, event) -> {
            if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
                String fString = etInput.getText().toString().replace("\n", "");
                processScan(fString);
            }
            etInput.setText("");
            etInput.requestFocus();
            return false;
        });

Where process scan is where you should do your lookup work. 应该在哪里进行进程扫描的地方进行查找工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM