我的Android应用程序布局非常简单。一个按钮(scan_barcode1),后跟edittext(scan_content)和另一个按钮(scan_barcode2),后跟另一个editext(scan_content2)。
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/scanner"
android:id="@+id/scan_barcode1"
android:layout_below="@+id/spinner7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="25dp"
android:onClick="openScanner1" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/scan_content"
android:layout_below="@+id/scan_barcode1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="@string/serial_number"
android:layout_alignRight="@+id/spinner7"
android:layout_alignEnd="@+id/spinner7" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/scanner"
android:id="@+id/scan_barcode2"
android:layout_below="@+id/scan_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="openScanner2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/scan_content2"
android:layout_below="@+id/scan_barcode2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="@string/serial_number"
android:layout_alignRight="@+id/scan_content"
android:layout_alignEnd="@+id/scan_content" />
我希望我的应用程序做什么:按下按钮后,扫描功能将打开,扫描条形码并将序列号放在其下方的编辑文本中。例如,如果按下scan_barcode1,序列号将被放置在scan_content中,如果按下scan_barcode2,序列号将出现在scan_content2中。
我的APP做了什么:按下按钮后,扫描功能将打开,扫描条形码放在活动中的两个editext中。例如,如果我按下scan_barcode1,则序列号将放在scan_content和scan_content2中。
以下是调用条形码扫描器的意图的代码。我正在使用zxing库fyi。
//This method opens the barcode scanner when scan_barcode1 button is pressed
public void openScanner1 (View view) {
IntentIntegrator scanIntegrator = new IntentIntegrator (this);
scanIntegrator.initiateScan();
}
//This method opens the bar code scanner when scan_barcode2 is pressed
public void openScanner2 (View view) {
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
我将两个editText初始化为contentTxt和contentTxt2
contentTxt = (EditText) findViewById(R.id.scan_content);
contentTxt2 = (EditText) findViewById(R.id.scan_content2);
然后我开始使用OnActivityResult方法来检索edittext,并将其放在正确的edittext中(它不会)
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
//Places barcode in the scan_content editext
if (scanningResult != null) {
String scanContent = scanningResult.getContents();
contentTxt.setText(scanContent);
}
//places bar code in the scan_content2 edittext
if (scanningResult !=null) {
String scanContent = scanningResult.getContents();
contentTxt2.setText(scanContent);
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
相反,条形码只是在两个edittext上复制。我已经坚持这个问题一段时间了,我迫切希望找到解决方案。非常感谢任何帮助!
所以我已经更新了我的代码但是现在我没有检索条形码而是因为结果而得到null,并且弹出了我的toast消息。有谁知道发生了什么?无论我按什么按钮,都会发生这种情况。
private int scanFieldPosition = 0;
public void openScanner1 (View view) {
scanFieldPosition = 1;
IntentIntegrator scanIntegrator = new IntentIntegrator (this);
scanIntegrator.initiateScan();
}
public void openScanner2 (View view) {
scanFieldPosition = 2;
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
if (scanningResult != null && scanFieldPosition == 1) {
String scanContent = scanningResult.getContents();
contentTxt.setText(scanContent);
}
if (scanningResult !=null && scanFieldPosition == 2) {
String scanContent = scanningResult.getContents();
contentTxt2.setText(scanContent);
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
问题是我没有变量static。我仍然得到吐司的消息,但我的条形码放在正确的edittext中,这很奇怪。无论如何,我的应用程序现在工作了,谢谢你的帮助!
private static int scanFieldPosition = 0;
答案 0 :(得分:1)
您可以为此设置static
标记,并在按下button1时为其分配1
,并在按下button2时为其指定2
。然后,您可以检查onActivityResult()
中的旗帜。你可以这样做:
public static int editTextNumber = 0;
public void openScanner1 (View view) {
IntentIntegrator scanIntegrator = new IntentIntegrator (this);
scanIntegrator.initiateScan();
editTextNumber = 1;
}
public void openScanner2 (View view) {
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
editTextNumber = 2;
}
然后在onActivityResult()
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
//Places barcode in the scan_content editext
Log.e("editTextNumber",""+ editTextNumber ); // check whats value here
if (scanningResult != null && editTextNumber == 1) { // checking for flag here
String scanContent = scanningResult.getContents();
contentTxt.setText(scanContent);
}
//places bar code in the scan_content2 edittext
else if (scanningResult !=null && editTextNumber == 2) { // checking for flag also
String scanContent = scanningResult.getContents();
contentTxt2.setText(scanContent);
}
else{
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
答案 1 :(得分:1)
如果scanningResult
不为空(换句话说,如果if
子句的条件为真),则为两个EditTexts设置文本。
您需要在创建扫描意图时保存标识要填充的EditText
的某种信息,并在onActivityResult
中检索该信息,例如:
private int scanFieldPosition = 0;
public void openScanner1 (View view) {
scanFieldPosition = 1;
IntentIntegrator scanIntegrator = new IntentIntegrator (this);
scanIntegrator.initiateScan();
}
public void openScanner2 (View view) {
scanFieldPosition = 2;
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
scanIntegrator.initiateScan();
}
然后检查scanFieldPosition
中的onActivityResult
,了解要填写的EditText
。
答案 2 :(得分:1)
您没有在IntentIntegrator中显示您的扫描意图构造,但我猜测您正在使用相同的requestCode。
将扫描意图的构造更改为具有不同的requestCode(可能除了上下文之外还可以通过将值传递给IntentIntegrator),然后在调用onActivityResult时,您将能够检查这一点以了解哪个扫描按下按钮并填充相关的EditText。
这是requestCode的设计目的,而非持有可能导致州问题的变量。