Dialog opens blank

2019-03-02 06:57发布

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!

1条回答
beautiful°
2楼-- · 2019-03-02 07:31

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.

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?

Something like this:

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

To run:

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

    @Override
    public void run() {
        DialogRunner.createAndShowDialog(ResourceManager.getInstance().activity, number);
    }

});
查看更多
登录 后发表回答