osc*_*rgc 5 android send imageview android-activity
我需要发送一个像我发送字符串"标题"的imageview,但我不能,我怎样才能将主视图中的imageview(R.drawable.image)发送到第二个活动?
谢谢
主要活动
public void NewActivity(View view){
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("title", getString(R.string.title));
startActivity(intent);
}
Run Code Online (Sandbox Code Playgroud)
第二次活动
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.pantalla);
Bundle extras = getIntent().getExtras();
if (extras == null)
{
return;
}
String title = extras.getString("title");
TextView textview = (TextView)findViewById(R.id.titulo);
textview.setText(title);
}
Run Code Online (Sandbox Code Playgroud)
解决方案1 :(对于非drawable资源)
您可以将路径文件名作为字符串发送.就像"title"你的例子中一样.
如果您对ImageView使用filepath有问题. 从文件路径显示图像视图?
解决方案2: (为drawable方便&光路)
发送资源整数值,如:
主要活动
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("resourseInt", R.drawable.image);
startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
第二次活动
@Override
public void onCreate(Bundle bundle)
{
super.onCreate(bundle);
setContentView(R.layout.pantalla);
Bundle extras = getIntent().getExtras();
if (extras == null)
{
return;
}
int res = extras.getInt("resourseInt");
ImageView view = (ImageView) findViewById(R.id.something);
view.setImageResourse(res);
}
Run Code Online (Sandbox Code Playgroud)