我在Android应用程序上编程,我有一个带有qr扫描程序的程序。当扫描仪检测到扫描时会发生某些事情。我的问题是我有多个qr代码,我需要程序识别正在扫描哪个qr代码。 在我的代码中,我有一个字符串"内容"我设置为相等" SCAN_RESULT"这与每个qr代码不同。 我通过在logCat中打印看到结果,并且我还使textView与内容相同。在这两种情况下," SCAN_RESULT"是" Scanextra1"对于第一个qr代码。 然后我说:if(contents ==" Scanextra1"){应该发生的事情}。但事实并非如此。 有人可以帮我吗?
我遇到问题的代码如下:
if (resultCode1 == RESULT_OK) {
contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
int QR = 0;
Log.v(contents, "QR value" + QR);
proeve.setText(contents);
//String contents2 = proeve.getText().toString();
if (contents == "Scanextra1"){...
活动的整个代码如下所示:
package your.choko.namespace;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.util.Log;
public class ScanExtra extends Activity {
String contents;
public static final String PREFS_NAME = "MyPrefsFile";
boolean extra1, extra2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scanextra);
SharedPreferences settings = getSharedPreferences("MyPrefsFile", 0);
extra1 = settings.getBoolean("fact1", false);
extra2 = settings.getBoolean("fact2", false);
Button scan = (Button) findViewById(R.id.buttonscanextra);
scan.setOnClickListener(new OnClickListener() {
static final int QR_ACTIVITY = 0;
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, QR_ACTIVITY);
}
});
}
/** Called when the activity is first created. */
@Override
public void onPause() {
super.onPause();
}
@Override
public void onStop() {
super.onStop();
}
public void onActivityResult(int requestCode, int resultCode1, Intent intent) {
TextView proeve = (TextView) findViewById(R.id.textView1);
if (requestCode == 0) {
if (resultCode1 == RESULT_OK) {
contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
int QR = 0;
Log.v(contents, "QR value" + QR);
proeve.setText(contents);
if (contents == "Scanextra1"){
//WHAT HAPPENS ON SCAN 1
SharedPreferences settings = getSharedPreferences("MyPrefsFile",0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("fact1", true);
editor.commit();
Toast toast = Toast.makeText(ScanExtra.this, contents, 9000);
toast.show();
startActivity(new Intent(ScanExtra.this, ScanExtraResultat.class));
}
}
}
}
}
答案 0 :(得分:0)
您不应该使用==
进行字符串比较。请改用equals()
:
if (contents.equals("Scanextra1")) {
...
}
使用==
时,检查这两个对象是否完全相同。使用equals()
时,检查两个字符串的内容是否相同。