在对话框和活动之间传递数据
问题描述:
我是android.I的新手,我正在开发一个小的基本app.I在应用程序中有四个按钮。我需要的是当用户按下按钮时,我想要一个对话框显示完整屏幕和特定的一组text views
。例如: 我有以下按钮,如: 1)英雄 2)电影。在按英雄按钮时,英雄列表应该类似地出现在电影中。我知道的唯一方法是为每个按钮按下创建一个新的活动。而我想要为所有四个按钮显示单个对话框,但是对话框上显示的数据应该根据按钮而变化。这可能吗?我知道这个问题是漫长的,可能更好way.Please被要求给我任何建议在对话框和活动之间传递数据
答
创建与所需数量的TextView
S创建自定义Dialog
一个方法,并通过您的String
值的方法
参考这个例子中
dialog_layout
<ImageView
android:id="@+id/imageDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="6dp" />
<TextView
android:id="@+id/textDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="@+id/imageDialog"/>
<Button
android:id="@+id/declineButton"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Submit "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="@+id/textDialog"
android:layout_toRightOf="@+id/imageDialog"
/>
广场
private void showDialog(){
final Dialog dialog = new Dialog(CustomDialog.this);
// Include dialog.xml file
dialog.setContentView(R.layout.dialog);
// Set dialog title
dialog.setTitle("Custom Dialog");
// Set Dialog fullscreen
dialog.getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);
// set values for custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.textDialog);
text.setText("Custom dialog Android example.");
ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog);
image.setImageResource(R.drawable.image0);
dialog.show();
Button declineButton = (Button) dialog.findViewById(R.id.declineButton);
// if decline button is clicked, close the custom dialog
declineButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Close dialog
dialog.dismiss();
}
});
}
这种活性showDialog()
方法请参考这里http://androidexample.com/Custom_Dialog_-_Android_Example/index.php?view=article_discription&aid=88
如果你显示的对话框全屏完整的例子,为什么你希望它是一个对话框?似乎你首先想到展示一项新活动是最好的。也许你可以解释为什么对话是至关重要的,以获得更好的建议。 – seekingStillness