java - 使用 Zxing 条形码的 1 个 Activity 的多个 onActivityResult

标签 java android-studio android-activity zxing

我正在开发一个简单的应用程序,用于从电脑收集 Assets 数据,每个 Assets 都有两个条形码。条形码是序列号和型号。该表单包含 2 个 TextView 和 3 个按钮。每个 TextView 返回每个 TextView 的结果扫描。同时,2 个按钮用于扫描条形码,1 个按钮用于将扫描结果提交到数据库。

下面的代码运行良好,但是当我使用任何“扫描”时,按钮总是将结果返回到序列号的第一个 TextView 。

public class MainActivity extends AppCompatActivity {

String scannedData;
TextView  contentTxt;
TextView  contentTxt2;

Button scanBtnSerial;
Button scanBtnModel;


//Button viewBtn;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Activity activity =this;
    scanBtnSerial = (Button)findViewById(R.id.scan_btn);//to scan serial number
    scanBtnModel = (Button)findViewById(R.id.scan2_btn);//to scan model number

    contentTxt = (TextView)findViewById(R.id.textView);//to view scanned serial number
    contentTxt2 = (TextView)findViewById(R.id.textView3);//to view scanned model number

    final TextView textView=(TextView)findViewById(R.id.textView);

    scanBtnSerial.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            IntentIntegrator integrator = new IntentIntegrator(activity);
            integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
            integrator.setPrompt("Scan Serial Number");
            integrator.setBeepEnabled(false);
            integrator.setCameraId(0);
            integrator.setBarcodeImageEnabled(false);
            integrator.initiateScan();

        }
    });

    scanBtnModel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            IntentIntegrator integrator = new IntentIntegrator(activity);
            integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
            integrator.setPrompt("Scan Model Number");
            integrator.setBeepEnabled(false);
            integrator.setCameraId(0);
            integrator.setBarcodeImageEnabled(false);
            integrator.initiateScan();

        }
    });



}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    IntentResult result = IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
    if(result!=null) {
        scannedData = result.getContents();
        if (scannedData != null) {

            Log.d("MainActivity", "Scanned");
            contentTxt.setText(result.getContents());
            new SendRequest().execute();//send to database 


        }else {
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

有人可以建议我如何管理“onActivityResult”以显示每次扫描的数据吗?

UI sample

最佳答案

快速而简单的方法是在 onActivityResult 中替换这部分

Log.d("MainActivity", "Scanned");
contentTxt.setText(result.getContents());
new SendRequest().execute();//send to database 

Log.d("MainActivity", "Scanned");
if(TextUtils.isEmpty(contextText.getText())){
    contentTxt.setText(result.getContents());
} else {
    contentTxt2.setText(result.getContents());
}
new SendRequest().execute();//send to database 

但是,这很可能不是您想要/需要的。您应该区分结果中获得的输入并将它们分配给适当的 TextView

如果您的代码具有不同的预定义格式,请考虑使用正则表达式来识别扫描了哪一个代码并将其分配给正确的TextView。 Android 有一个名为 Pattern 的辅助类,检查一下。

生成的代码可能如下所示:

Log.d("MainActivity", "Scanned");
if(isSerialNumberString(result.getContents())){
    contentTxt.setText(result.getContents());
} else if(isModelNumberString(result.getContents())) {
    contentTxt2.setText(result.getContents());
}
new SendRequest().execute();//send to database 

如果没有,考虑使用设置流程,您要求用户首先扫描一个代码,将其放入用户界面中,然后要求他们扫描第二个代码。这样您就会知道您现在需要哪个代码。

<小时/>

编辑: 事实证明,ZXing 有一个机制可以实现这一点。因此,您需要做两件事:

首先,将自定义请求代码添加到您的集成商。因此 ScanBtnSerial 的 OnClickListener 将如下所示:

IntentIntegrator integrator = new IntentIntegrator(activity);
integrator.setRequestCode(1) //Make this a constant        
integrator.setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES);
integrator.setPrompt("Scan Model Number");

对另一个 OnClickListener 执行相同的操作,只需将 requestCode 设置为 2。

然后,在 onResult 方法中执行以下操作:

Log.d("MainActivity", "Scanned");
if(requestCode == 1) { //Again, use a const here
    contentTxt.setText(result.getContents());
} else if(requestCode == 2) {
    contentTxt2.setText(result.getContents());
}
new SendRequest().execute();//send to database 

关于java - 使用 Zxing 条形码的 1 个 Activity 的多个 onActivityResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49168453/

相关文章:

java - Simba][SpannerJDBCDriver](100300) 来自服务器的错误消息 : INVALID_ARGUMENT: Positional parameters are not supported

java - Java中获取XML文件的属性

Android 探查器不显示 fragment 生命周期

android - 方法 FloatMath.sqrt() 已弃用

android - 在 Activity 中使用 intent.getStringExtra

java - 主类中的变量

java - 我的 Ubuntu 安装上的 Java 版本是 "1.8.0_191"?

android - 开始虚拟 Activity

android - NDK r10 b 32 位或 64 位或使用两者编译以及如何实现

android - 如何清除启动 Activity 的 Intent?