簡體   English   中英

掃描時刪除條形碼字符

[英]Drop barcode character on scanning

我在主要活動中有一個帶有條形碼掃描儀的android應用程序。 掃描條形碼后,我在數據庫中搜索此號碼。

我的問題是,當我掃描條形碼時,不會讀取所有字符。 例如,如果我嘗試掃描807251306246 ,則在掃描此條形碼后,將隨機刪除一個或兩個字符。

我試圖創建一個新的應用程序,該應用程序讀取條形碼並在列表中查看它。 工作正常。 我在兩個應用程序中使用相同的代碼。

編碼:

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檔案:

<?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>

此代碼接受輸入到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;
        });

應該在哪里進行進程掃描的地方進行查找工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM