2017-02-23 134 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設備)。你會想考慮將圖像存儲到數據庫或另一種方法,不涉及傳遞大對象,如創建一個持有對象的單例類。

如果這是一個可繪製的資源,你可以參考艾哈邁德的答案。

相關問題