我正在尝试使用微调器在一个活动中修改ImageView的图像,然后将该图像发送到另一个活动中的ImageButton。
以下是ImageView活动的代码:
imgView = (ImageView) findViewById(R.id.imgView);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Intent imgIntent = getIntent();
switch (position) {
case 0:
imgView.setImageResource(R.drawable.brown);
imgIntent.putExtra("image", R.drawable.brown);
break;
case 1:
imgView.setImageResource(R.drawable.blue);
imgIntent.putExtra("image", R.drawable.blue);
break;
case 2:
imgView.setImageResource(R.drawable.black);
imgIntent.putExtra("image", R.drawable.black);
break;
case 3:
imgView.setImageResource(R.drawable.white);
imgIntent.putExtra("image", R.drawable.white);
break;
default:
imgView.setImageResource(R.drawable.brown);
imgIntent.putExtra("image", R.drawable.brown);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
以下是ImageButton活动的代码:
ImageButton imgBtn = (ImageButton) findViewById(R.id.imgBtn);
Intent imgIntent = getIntent();
imgBtn.setImageResource(imgIntent.getIntExtra("image"));
我无法弄清楚如何做到这一点。
MainActivity的完整代码:
公共类MainActivity扩展了AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton imgBtn = (ImageButton) findViewById(R.id.imgBtn);
Intent imgIntent = getIntent();
imgBtn.setImageResource(imgIntent.getIntExtra("image", 0));
startActivity(imgIntent);
imgBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), CustomizeDoggo.class);
startActivity(intent);
}
});
}
}
答案 0 :(得分:0)
只需更改此行。您尚未通过默认值
imgBtn.setImageResource(imgIntent.getIntExtra("image",0));
你还没有开始活动
startActivity(mIntent);
<强>更新强>
像这样开始一个意图
//This line is missing in your code
Intent intent = new Intent(CurrentActivity.this,ActivityToOpen.class);
intent.putExtra("image",id of selected image);
startActivity(intent);
答案 1 :(得分:0)
在dest活动中:
在getArgurment()
方法中调用onCreate()
来获取包,然后getIntExtra("image",default value);
答案 2 :(得分:0)
等一下这肯定会有用
在开始新活动时的主要活动中,使用此代码而不是您的代码
Intent=new Intent(mContext,SecondActivity.class);
intent.putExtra("image",R.drawable.blue);
startActivity(intent);
现在在oncreate的第二个活动中使用此代码
int image=getIntent().getExtras().getInt("image",0);
mPlayPauseImageButton.setImageResource(image);