0
我正在研究一個應用程序,它在點擊按鈕時調用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
}
}
}
}
和被叫活動命名結果
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
}
}
}
}
謝謝,但我真的不明白那個項目在做什麼,你不知道爲什麼我的代碼不工作,是否有掃描代碼格式的東西?它不是一個字符串? – 2011-03-12 20:44:49
我修好了。我寫了意圖,而不是結果,但現在它運作良好,我的方式。沒有任何lib。謝謝您的合作。接受答案。 – 2011-03-12 22:26:47