我正在开发一个ZXing
在单击按钮时调用扫描仪的应用程序。效果很好,在扫描代码后会弹出一个新活动,该活动应该获取扫描的代码,所以我可以编辑它,输入我想要从该产品中获得的数量等。但我不知道如何进行扫描代码。这是我到目前为止所拥有的:
package org.example.sudoku;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.app.AlertDialog;
public class Sudoku extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up click listeners for all the buttons
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
View scanButton = findViewById(R.id.scan_button);
scanButton.setOnClickListener(this);
View editButton = findViewById(R.id.about_button);
editButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.scan_button:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
break;
case R.id.about_button:
Intent about = new Intent(this.getApplicationContext(),About.class);
Bundle b = new Bundle();
b.putString("key","blablabla");
about.putExtras(b);
startActivityForResult(about, 0);
break;
case R.id.exit_button:
finish();
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
Intent result = new Intent(this.getApplicationContext(),Result.class);
Bundle b = new Bundle();
b.putString("contents",contents);
intent.putExtras(b);
startActivityForResult(result, 0);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
}
以及名为 Result 的被调用活动
package org.example.sudoku;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.content.Intent;
public class Result extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
View finishButton =findViewById(R.id.finish_button);
finishButton.setOnClickListener(this);
View nextButton =findViewById(R.id.next_button);
nextButton.setOnClickListener(this);
Bundle b = getIntent().getExtras();
String product = b.getString("contens").toString();
EditText et1 = (EditText) findViewById(R.id.edit_text);
et1.setText(product);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.finish_button:
finish();
break;
case R.id.next_button:
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("com.google.zxing.client.android.SCAN.SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
finish();
break;
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan
Intent result = new Intent(this.getApplicationContext(),Result.class);
Bundle b = new Bundle();
b.putString("contents",contents);
intent.putExtras(b);
startActivityForResult(result, 0);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel
}
}
}
}