gpt4 book ai didi

java - 2 个扫描仪但有一个 onActivityResult?

转载 作者:行者123 更新时间:2023-12-01 17:53:07 26 4
gpt4 key购买 nike

我正在 Android Studio 中开发移动应用程序,并且有 2 个按钮。每个按钮都将使用 Zxing 库启动条形码/QR 扫描。

我遇到的问题是我将第一次扫描的结果分配给按钮文本,并且工作正常。

但是,当将第二次扫描的结果分配给其按钮时,它不起作用,因为只有一个 onActivityResult?

我知道,如果我复制并粘贴代码并尝试覆盖 onActivityResult2,系统将忽略它,因为系统无法识别它。

但是,我该如何解决这个问题呢?

这是我的 onActivityResult 和 onActivityResult2 代码,只是为了展示我想要实现的目标:

Activity 结果:

工作代码:

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

if (result != null) {
//if qrcode has nothing in it
if (result.getContents() == null) {
Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
} else {
//if qr contains data
try {
//converting the data to json
JSONObject obj = new JSONObject(result.getContents());

} catch (JSONException e) {
e.printStackTrace();
if (buttonId == R.id.buttonScan) {
machineType = result.getContents();
scanBarcode.setText("Machine Type: " + machineType);
} else if (buttonId == R.id.buttonScan2) {
workOrderNumber = result.getContents();
scanWorkOrder.setText("Work Order Number: " + workOrderNumber);
}
else if (buttonId == 0) {
???
}

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

调用库的按钮:

private IntentIntegrator qrScan;
private IntentIntegrator qrScan2;

int buttonId = 0; //THIS IS GLOBAL

//Initialise Buttons
scanBarcode = findViewById(R.id.buttonScan);
scanWorkOrder = findViewById(R.id.buttonScan2);
submit = findViewById(R.id.buttonSubmit);

sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

//Initialize Scanners
qrScan = new IntentIntegrator(this);


scanBarcode.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
qrScan.initiateScan();
buttonId = R.id.buttonScan;
}
});

scanWorkOrder.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
qrScan.initiateScan();
buttonId = R.id.buttonScan2;

}
});

最佳答案

在遵循重载规则时,您不能拥有两个完全相同的方法,因此

对于两个请求,您可以有两个不同的requestCode,然后使用if语句来识别您的请求类型或按钮点击响应

startActivityForResult (intent, 100); // 100 for button 1
startActivityForResult (intent, 101); // 101 for button 2

ActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
//if qrcode has nothing in it
if (result.getContents() == null) {
Toast.makeText(this, "Result Not Found", Toast.LENGTH_LONG).show();
} else {
//if qr contains data
try {
//converting the data to json
JSONObject obj = new JSONObject(result.getContents());

} catch (JSONException e) {
e.printStackTrace();

workOrderNumber = result.getContents();
if(requestCode == 100)
scanBarcode.setText("Machine Type: " + machineType);
else if(requestCode == 101)
scanWorkOrder.setText("Work Order Number: " + workOrderNumber);
}
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}

关于java - 2 个扫描仪但有一个 onActivityResult?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47778081/

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