Java 在android中从列表打开同一对话框
我有一个包含许多项目的listActivity 对于每个项目,我希望在发送项目id\position\other时打开相同的弹出窗口 该项目的唯一信息对象 但基本上每次我都会打开完全相同的弹出窗口 其按钮将向服务器发送额外的唯一数据 我读了一些教程,看到一个对话框通常是这样打开的:Java 在android中从列表打开同一对话框,java,android,android-layout,dialog,Java,Android,Android Layout,Dialog,我有一个包含许多项目的listActivity 对于每个项目,我希望在发送项目id\position\other时打开相同的弹出窗口 该项目的唯一信息对象 但基本上每次我都会打开完全相同的弹出窗口 其按钮将向服务器发送额外的唯一数据 我读了一些教程,看到一个对话框通常是这样打开的: final Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.custom); dialog.setTitle("Title...
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...");
}
我认为创建不同于对话框的文件更具可读性
像这样:
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上得到了一个语法错误
创建新文件是否常见且良好?如果是这样的话,如何正确地做到这一点
它是否更高效?只是显示相同的对话框,而不是每次初始化一个新的对话框?或者两种方法都一样?对于语法错误,请确保您是从应用程序导入R类,而不是Android SDK类R资源 对于一个有经验的程序员来说,你的直觉是将对话框移动到一个单独的文件中,尤其是当它包含大量代码时。有时这可能是正确的决定 但是,将对话框作为嵌入式类或像示例中那样内联创建它通常很方便,因为这样它就在活动的范围内,并且可以访问活动本身上的状态变量和受保护的方法
因此,如果您将这些信息保存在一个单独的文件中,那么您最终会将其传递给对话框,在某些情况下,这样做是不值得的,因为代码变得更加复杂且不易维护。最好参见DialogFragment。onCreate方法中出现了什么语法错误?