[英]Zxing barcode matching app for android
我正在嘗試開發一個應用程序,該應用程序將掃描條形碼,將值存儲在變量中,然后掃描另一個條形碼,並將該值與先前的值進行匹配,並返回“匹配”或“不匹配”結果。 感謝foamilyguy的幫助,第一部分已經開始工作,這是我使用的初始代碼,
package com.barcodesample;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class BarcodeSample extends Activity {
private Button scanBtn;
private TextView resultsTxt;
/** Called when the activity is first created. */;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
scanBtn = (Button) findViewById(R.id.scanBtn);
resultsTxt = (TextView) findViewById(R.id.resultsTxt);
scanBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
resultsTxt.setText("Scanning...");
Intent intent = new intent("com.google.zxing.client.android.SCAN");
try {
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
new AlertDialog.Builder(v.getContext())
.setTitle("WARNING:")
.setMessage("You don't have Barcode Scanner installed. Please install it.")
.setCancelable(false)
.setNeutralButton("Install it now", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Uri uri = Uri.parse("market://search?q=pname:com.google.zxing.client.android");
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
})
.show();
}
}
});
}
/*Here is where we come back after the Barcode Scanner is done*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// contents contains whatever the code was
String contents = intent.getStringExtra("SCAN_RESULT");
// Format contains the type of code i.e. UPC, EAN, QRCode etc...
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
// Handle successful scan. In this example I just put the results into the TextView
resultsTxt.setText(format + "\n" + contents);
} else if (resultCode == RESULT_CANCELED) {
// Handle cancel. If the user presses 'back' before a code is scanned.
resultsTxt.setText("Canceled");
}
}
}
}
現在,我如何使用與上面類似的另一個掃描按鈕開始另一個意圖,最后如何比較兩個值並在新屏幕中返回匹配項或不匹配項?
在此先感謝您的寶貴建議
除以下內容外,在其他按鈕上使用相同的代碼。
startActivityForResult(intent, 1); // right now it's 0 for button 1
現在,在onActivityResult中再添加一個部分,以返回第二個按鈕的結果。
if (requestCode == 1) {
// your code here that will be used for comparing.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.