使用按钮将数据从一个视图发送到另一个视图
sending data from one view to another view with a button
我有一个按钮,它是表格行中的最后一个元素,如下所示:
<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:text="@string/person1_name"
来自其中一个 TextView
还有这个:
android:src="@drawable/profile_one"
来自 ImageView。
我试过在按钮代码中使用标签作为名称:
String text_tag = view.getTag().toString();
intent.putExtra(EXTRA_NAME, text_tag);
这确实有效,但我不知道如何从 ImageView 发送图像。
有没有办法同时发送两者?
谢谢!
您也可以在图像视图上将可绘制对象 ID 设置为标签,并通过 Intent 将其作为额外内容发送给其他 Activity。
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);
}
假设您正在谈论的图像不是可绘制资源,就像您将 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 个设备)。您可能需要考虑将图像存储到数据库中或其他不涉及传递大型对象的方法,例如创建一个保存对象的单例 class。
如果是drawable资源,可以参考Ahmed的回答
我有一个按钮,它是表格行中的最后一个元素,如下所示:
<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:text="@string/person1_name"
来自其中一个 TextView
还有这个:
android:src="@drawable/profile_one"
来自 ImageView。
我试过在按钮代码中使用标签作为名称:
String text_tag = view.getTag().toString();
intent.putExtra(EXTRA_NAME, text_tag);
这确实有效,但我不知道如何从 ImageView 发送图像。
有没有办法同时发送两者?
谢谢!
您也可以在图像视图上将可绘制对象 ID 设置为标签,并通过 Intent 将其作为额外内容发送给其他 Activity。
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);
}
假设您正在谈论的图像不是可绘制资源,就像您将 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 个设备)。您可能需要考虑将图像存储到数据库中或其他不涉及传递大型对象的方法,例如创建一个保存对象的单例 class。
如果是drawable资源,可以参考Ahmed的回答