简体   繁体   English

如何扫描多个QR码并获取附加码?

[英]How do i scan multiple QR codes and get the addition?

I have n QR codes containing price in integers. 我有n个包含整数价格的QR码。 I want to scan each qr code and add its price to the total amount. 我想扫描每个二维码,并将其价格加到总额中。 Here is MainActivity.java eg. 这是MainActivity.java例如。 If i scan a qr code containing price 40, 40 gets displayed in TextView2. 如果我扫描包含价格40的二维码,则在TextView2中显示40。 how do i add n qr codes and update the total each time i scan a qrcode? 每次扫描二维码时,如何添加n个二维码并更新总数?

package com.mycompany.smartshoppingcart;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private static final int ACTIVITY_RESULT_QR_DRDROID = 0;

TextView report;
Button scan;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    report = (TextView) findViewById(R.id.textView2);
    scan = (Button) findViewById(R.id.button1);

    scan.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Intent i = new Intent("la.droid.qr.scan");

            try {

                startActivityForResult(i, ACTIVITY_RESULT_QR_DRDROID);
            }
            catch (ActivityNotFoundException activity) {

                MainActivity.qrDroidRequired(MainActivity.this);
            }
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if( ACTIVITY_RESULT_QR_DRDROID == requestCode
            && data != null && data.getExtras() != null ) {

        String result = data.getExtras().getString("la.droid.qr.result");



      report.setText(result);
        report.setVisibility(View.VISIBLE);
    }
}

/*
 *
 * If we don't have QRDroid Application in our Device,
 * It will call below method (qrDroidRequired)
 *
 */

protected static void qrDroidRequired(final MainActivity activity) {
    // TODO Auto-generated method stub

    AlertDialog.Builder AlertBox = new AlertDialog.Builder(activity);

    AlertBox.setMessage("QRDroid Missing");

    AlertBox.setPositiveButton("Direct Download", new OnClickListener() {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            // TODO Auto-generated method stub

            activity.startActivity(new Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://droid.la/apk/qr/")));
        }
    });

    AlertBox.setNeutralButton("From Market", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

            activity.startActivity(new Intent(Intent.ACTION_VIEW,
                    Uri.parse("http://market.android.com/details?id=la.droid.qr")));
        }
    });

    AlertBox.setNegativeButton("Cancel", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

            dialog.cancel();
        }
    });

    AlertBox.create().show();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);


    return true;
}

} }

You should just store this value in a global variable for the class. 您应该只将该值存储在该类的全局变量中。

at top of class add int totalCost = 0; 在类顶部添加int totalCost = 0; (below Button scan; line) (在Button scan;下方Button scan;行)

Then in onActivityResult , once you get the result back from the qrcode you should cast this variable to an integer 然后在onActivityResult ,一旦您从qrcode返回结果,就应该将此变量转换为整数

via int cost = Integer.parseInt(result); 通过int cost = Integer.parseInt(result);

Once you have the integer value of the cost just add it to the total cost 获得成本的整数值后,只需将其添加到总成本中

totalCost += cost;

Then update the text label 然后更新文字标签

report.setText("" + totalCost);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM