2013-10-17 134 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