gpt4 book ai didi

java - 切换 Activity/在 Activity 之间传递数据

转载 作者:行者123 更新时间:2023-12-01 16:03:46 25 4
gpt4 key购买 nike

因此,根据我提出的最后一个问题的建议,我弄清楚了如何调用 BarCodeScanner 并将值返回到字段。所以现在,我有一个 toast ,上面写着“扫描成功”,然后我想将结果传递给新的 Activity 。当我注释掉我的 Intent 时,一切正常(显然,减去数据的传递/屏幕的切换),但是当我按原样运行我的项目时,它 FC ...... eclipse 在代码或 XML 中没有报告错误。有什么见解吗?

package com.mhe.test.scan;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button myScanButton = (Button) findViewById(R.id.myScanButton);
totalbox = (EditText) findViewById(R.id.tBox);

myScanButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
}
});
}
private EditText totalbox;
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
final String contents = intent.getStringExtra("SCAN_RESULT");

if ( totalbox != null )
totalbox.setText(contents);


Context context = getApplicationContext();
CharSequence text = "Successful Scan";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();


Intent i = new Intent(main.this, Result.class);
i.putExtra("SNARP", "SCAN_RESULT");
startActivityForResult(i, 0);

} else if (resultCode == RESULT_CANCELED) {

if ( totalbox != null )
totalbox.setText("bummer");
}
}
}

}

然后在新 Activity 中处理正在传递的数据:

package com.mhe.test.scan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Result extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);

Intent i = getIntent();
Bundle b = i.getExtras();
String foosh = b.getString("SNARP");

EditText box1 = (EditText) findViewById(R.id.tBox1);
box1.setText(foosh);

最佳答案

尝试在调用新 Intent 时发送 Bundle 对象。

Intent i = new Intent(main.this, Result.class);
Bundle b = new Bundle();
b.putString("SNARP", "SCAN_RESULT")
i.putExtras(b);

关于java - 切换 Activity/在 Activity 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3153929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com