我最近切换到AndEngine玩这个引擎。在切换之前我已经实现了一个DialogFragment
工作得很好。现在我想把这个DialogFragment
“移植”到AndEngine。因为在AndEngine中不支持FragmentActivity(据我所知),我决定将代码更改为简单的Dialog
。
现在对话框打开很好,但是完全空白。只是一个带边框的小黑色矩形。
我看不出我的代码可能出现什么问题。可能你可以提供帮助。
public class SimpleDialog extends Dialog {
final long number;
Context context;
public SimpleDialog (Context context, long number) {
super(context);
this.number = number;
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = ResourceManager.getInstance().activity.getLayoutInflater();
final View view = inflater.inflate(R.layout.logindialog, null);
final EditText editTextUserName = (EditText) view.findViewById(R.id.usernameToSubmit);
final EditText editTextPassword = (EditText) view.findViewById(R.id.passwordToSubmit);
TextView numberText = (TextView) view.findViewById(R.id.numberText);
highscoreText.setText("Number: " + Long.toString(number));
builder.setView(view)
.setNegativeButton(R.string.login_submit, null)
.setPositiveButton(R.string.login_abort, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
final AlertDialog d = builder.create();
d.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button b = d.getButton(AlertDialog.BUTTON_NEGATIVE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
});
}
这就是我打开对话框的方式:
ResourceManager.getInstance().activity.runOnUiThread(new Runnable() {
@Override
public void run() {
new SimpleDialog(ResourceManager.getInstance().activity, number).show();
}
});
如果您还需要我的布局,请告诉我。 非常感谢!
答案 0 :(得分:1)
问题是您使用AlertDialog.Builder
而不是修改自己的Dialog实例。 AlertDialog.Builder
创建自己的AlertDialog,并修改该实例。它不会,也不会影响您的SimpleDialog
实例。
由于您已经在使用Builder创建Dialog,因此我认为不需要扩展Dialog
本身。为什么不将Builder代码移动到方法或包装类中,并直接调用该方法?
这样的事情:
public class DialogRunner {
public static void createAndShowDialog (Context context, long number){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = LayoutInflater.from (context);
final View view = inflater.inflate(R.layout.logindialog, null);
final EditText editTextUserName = (EditText) view.findViewById(R.id.usernameToSubmit);
final EditText editTextPassword = (EditText) view.findViewById(R.id.passwordToSubmit);
TextView numberText = (TextView) view.findViewById(R.id.numberText);
highscoreText.setText("Number: " + Long.toString(number));
builder.setView(view)
.setNegativeButton(R.string.login_submit, null)
.setPositiveButton(R.string.login_abort, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
final AlertDialog d = builder.create();
d.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
Button b = d.getButton(AlertDialog.BUTTON_NEGATIVE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
});
d.show();
}
}
运行:
ResourceManager.getInstance().activity.runOnUiThread(new Runnable() {
@Override
public void run() {
DialogRunner.createAndShowDialog(ResourceManager.getInstance().activity, number);
}
});