[英]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.