2017-02-23 116 views
0

我有一个按钮,是tablerow的内部的最后一个元素,例如:发送从一个视图数据到另一个视图与按钮

<TableRow 
    android:id="@+id/tableRow1" 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:layout_marginRight="8dp" 
    android:layout_marginLeft="8dp" 
    android:layout_marginTop="8dp" 
    android:layout_marginBottom="8dp"> 
<ImageView 
    android:id="@+id/person1_icon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="4dp" 
    android:tag="@drawable/profile_one" 
    android:src="@drawable/profile_one" 
    /> 
    <TableLayout 
     android:layout_width="200dp" 
     android:layout_height="match_parent" 
     android:layout_marginLeft="4dp" 
     android:orientation="horizontal"> 
     <TableRow android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:layout_marginRight="2dp" 
      android:layout_marginLeft="2dp" 
      android:layout_marginTop="2dp" 
      android:layout_marginBottom="2dp"> 
      <TextView android:id="@+id/person1_seek" 
       android:layout_weight="1" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:text="@string/person1_name" 
       android:hint="@string/person1_name" /> 
     </TableRow> 

    </TableLayout> 

    <Button 
     android:id="@+id/addButton1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:onClick="ShowView" /> 
</TableRow> 

该按钮的代码看起来是这样的:

public void ShowView(View view) { 
    Intent intent = new Intent(this, DisplaySeekAgain.class); 
    startActivity(intent); 
} 

当按下按钮,我需要从这个布局发送数据,以新的布局,特别是这样的数据:

的android:文本=“@字符串/ person1_na我”

从TextViews

这一个:

机器人:SRC = “@绘制/ profile_one”

从ImageView的。

我试图在按钮的代码使用标签像这样的名字:

String text_tag = view.getTag().toString(); 
    intent.putExtra(EXTRA_NAME, text_tag); 

这工作,但我无法弄清楚如何还从ImageView的发送图像。

有没有办法同时发送?

谢谢!

回答

2

您可以在图像视图中将可绘制标识设置为标记,并通过意图将其作为附加信息发送给其他活动。

public void ShowView(View view) { 
    Intent intent = new Intent(this, DisplaySeekAgain.class); 
    int drawableId = (Integer) myImageView.getTag(); 
    String textTag = (String) view.getTag(); 
    intent.putExtra(EXTRA_NAME, text_tag); 
    intent.putExtra(EXTRA_DRAWABLE_ID, drawableId); 
    startActivity(intent); 
} 
+0

如何在图像视图中传递myImageView?谢谢 – SkyeBoniwell

+0

@SkyeBoniwell imageView应该是一个全局变量,你应该在'setContentView()''myImageView =(ImageView)findViewById(R.id.person1_icon)'之后的onCreate() –

1

假设你正在谈论的图像不是绘制资源,就如同你传递的String当临时演员,你可以通过影像(位图)太。所以,从你的ImageView得到Bitmap这样的:

BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); 
Bitmap bitmap = drawable.getBitmap(); 

由于Bitmap实现Parcelable,你可以把它像演员

intent.putExtra(IMAGEVIEW_TAG, bitmap) 

只是一个建议,如果Bitmap太大你会开始遇到TransactionTooLarge例外,特别是如果您的目标为API level 25(Android N设备)。你会想考虑将图像存储到数据库或另一种方法,不涉及传递大对象,如创建一个持有对象的单例类。

如果这是一个可绘制的资源,你可以参考艾哈迈德的答案。

相关问题