2013-10-17 107 views
1

我最近切换到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(); 
    } 

}); 

,如果你还需要我的布局,请让我知道。 非常感谢!

回答

1

问题是,您正在使用AlertDialog.Builder而不是修改您自己的对话框实例。 AlertDialog.Builder创建它自己的AlertDialog,并修改实例。它不会,也不会影响你的SimpleDialog实例。

由于您已经在使用Builder创建对话框,因此我不认为需要扩展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); 
    } 

}); 
+0

感谢您的解释。它像一个魅力:) – puelo

+0

@puelo没问题:-) –

+0

.setNegativeButton(R.string.login_submit,null) .setPositiveButton(R.string.login_abort,new DialogInterface.OnClickListener(){should be .setNegativeButton(R .string.login_abort,null) .setPositiveButton(R.string.login_submit,new DialogInterface.OnClickListener(){ – jazz