我有两个包含整数价格的QR码。我想扫描每个qr代码并将其价格加到总金额中。这是MainActivity.java 例如。如果我扫描包含价格40的qr代码,则会在TextView2中显示40。如何添加n个qr代码并在每次扫描qrcode时更新总数?
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;
}
}
答案 0 :(得分:0)
您应该将此值存储在类的全局变量中。
在课程顶部添加int totalCost = 0;
(Button scan;
行以下)
然后在onActivityResult
中,一旦从qrcode返回结果,就应该将此变量转换为整数
通过int cost = Integer.parseInt(result);
获得成本的整数值后,只需将其添加到总费用
即可 totalCost += cost;
然后更新文本标签
report.setText("" + totalCost);