【发布时间】:2014-02-08 11:55:05
【问题描述】:
我有一个包含许多项目的 listActivity。
对于每个项目,我想在发送项目 id\position\other 时打开相同的弹出窗口
该项目独有的信息对象。
但基本上我总是打开完全相同的弹出窗口。
它的按钮会将额外的唯一数据发送到服务器。
我看了一些教程,看到一个对话框通常是这样打开的:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
我认为为对话框创建不同的文件更具可读性。
像这样:
public class SocialActionsDialog extends Dialog {
public SocialActionsDialog(Context context) {
super(context);
mContext = context;
}
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.social_actions);
setTitle("Title...");
}
然后我在OnCreate 上收到语法错误。
创建新文件是常见且良好的做法吗?如果是,如何正确地做到这一点?
它是否更有效——只显示相同的对话框而不是每次都初始化一个新对话框?还是两种方式都一样?
【问题讨论】:
-
最好查看 DialogFragment。你在 onCreate 方法中遇到了什么语法错误?
标签: java android android-layout dialog