我有一个Android应用程序,它有2个活动MainActivity和CarteActivity。我在MainActivity布局(activity_main.xml)上有一个按钮,它启动了一个QR码扫描器(已经使用zxing编程)。第二个活动布局(activity_carte.xml)有一个ImageView,代码为:
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/carte0" />
`
在我的drawable文件夹中,我复制了一些名为carte1,carte2,carte3 ...
的图片我还生成了一些qrcodes,结果也是carte1,carte2,carte3 ......(作为文本)
现在我希望当我扫描这些qrcodes时,CarteActivity出现并且ImageViewer显示与代码对应的图片(当我扫描返回carte1的代码时,图片carte1出现在ImageViewer中),但是我不知道怎么做。
我注意到在MainActivity中,扫描结果存储在字符串“contents”中:
public void onActivityResult(int requestCode, int resultCode, Intent intent)
{
if (requestCode == 0)
{
if (resultCode == RESULT_OK)
{
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Log.i("xZing", "contents: "+contents+" format: "+format);
// Handle successful scan
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Log.i("xZing", "Cancelled");
}
}
`
但我不知道如何正确使用它。
你可以帮帮我吗?我确定这并不难,但我找不到怎么做。答案 0 :(得分:0)
按照
更改您的MainActivity电话屁股public void onActivityResult(int requestCode, int resultCode, Intent intent){
if (requestCode == 0)
{
if (resultCode == RESULT_OK)
{
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
Log.i("xZing", "contents: "+contents+" format: "+format);
// Handle successful scan
Intent startNewActivityOpen = new Intent(MainActivity.this, CarteActivity.class);
startNewActivityOpen.putExtra("QRContents", contents);
startActivity(startNewActivityOpen);
}
else if (resultCode == RESULT_CANCELED)
{
// Handle cancel
Log.i("xZing", "Cancelled");
}
}
}
然后按照以下方式对CarteActivity进行更改
import android.content.Intent;
import android.widget.ImageView;
public class CarteActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_carte);
Intent bgIntent = getIntent();
String contents = bgIntent.getStringExtra("QRContents");
ImageView img = (ImageView)findViewById(R.id.imageView1);
if(contents.equals("carte1")){
img.setImageResource(R.drawable.carte1);
}
if(contents.equals("carte2")){
img.setImageResource(R.drawable.carte2);
}
if(contents.equals("carte3")){
img.setImageResource(R.drawable.carte3);
}
}
@Override
public void onBackPressed() {
Intent startNewActivityOpen = new Intent(CarteActivity.this, MainActivity.class);
startActivity(startNewActivityOpen);
}
}