对话框打开空白 [英] Dialog opens blank

查看:199
本文介绍了对话框打开空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近改用了AndEngine来玩这个引擎.在切换之前,我已经实现了DialogFragment,效果很好.现在我想将此DialogFragment移植"到AndEngine.因为在AndEngine中不支持FragmentActivity(据我所知),所以我决定将代码更改为简单的Dialog. 现在,对话框可以正常打开,但是完全空白.只是一个带有边框的黑色小矩形.

i recently switched to AndEngine to play a little bit around with this engine. Before the switch i already implemented a DialogFragment wich worked just fine. Now i wanted to "port" this DialogFragment to the AndEngine. Because there is no support for the FragmentActivity (as far as i know) in AndEngine i decided to change my code to a simple Dialog instead. Now the dialog opens just fine, but is completely blank. Just a small black rectangle with a border.

我看不到代码有什么问题.也许您可以帮忙.

I cannot see what could be wrong with my code..maybe you can help.

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) {

                    }
                });

            }
        });
    }

这是我打开对话框的方式:

This is how i open the Dialog:

ResourceManager.getInstance().activity.runOnUiThread(new Runnable() {

    @Override
    public void run() {
        new SimpleDialog(ResourceManager.getInstance().activity, number).show();
    }

});

如果您还需要我的布局,请告诉我. 非常感谢!

if you also need my layout please let me know. Thanks a lot!

推荐答案

问题是您正在使用AlertDialog.Builder而不是修改自己的Dialog实例. AlertDialog.Builder创建其自己的AlertDialog,并改为修改那个实例.不会,也不会影响您的SimpleDialog实例.

The problem is that you are using the AlertDialog.Builder rather than modifying your own instance of the Dialog. AlertDialog.Builder creates its own AlertDialog, and modifies that instance instead. It does not, and will not affect your SimpleDialog instance.

由于您已经在使用Builder来创建对话框,所以我不需要扩展Dialog本身.为什么不将Builder代码移动到方法或包装器类中,然后直接调用该方法?

Since you are already using the Builder to create your Dialog I don't see a need for extending Dialog itself. Why not move the Builder code into a method or a wrapper class, and call the method directly?

类似这样的东西:

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);
    }

});

这篇关于对话框打开空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆