2011-07-03 285 views
0

我想顯示一個簡單的自定義對話框。對於初學者,我只是想添加一個文本視圖並查看對話框是否顯示。Android自定義對話框不顯示

這是我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<TextView android:id="@+id/tvPreview" 
android:layout_height="wrap_content" 
android:layout_width="wrap_content" 
android:text="@string/Instructions"></TextView> 
</LinearLayout> 

這是我的onCreateDialog功能代碼:

@Override 
protected Dialog onCreateDialog(int id) { 
    final Dialog dialog = new Dialog(this); 
    dialog.setContentView(R.layout.predialog); 
    dialog.setTitle("Tests of my Dialog"); 
    return dialog; 
} 

當用戶(我)按下我使用此代碼菜單項:

public void DiagTests(){ 
    showDialog(0); 
} 

會發生什麼情況是屏幕模糊,但對話框不顯示。

有沒有人有任何想法我做錯了什麼?

PD:以防萬一沒有任何錯誤或警告。

感謝您的幫助

回答

1

您可以嘗試這種方法。創建自定義對話框類(這是一個類的實例,你可以使用你想要的):

/** Class Must extends with Dialog */ 
/** Implement onClickListener to dismiss dialog when OK Button is pressed */ 
public class DialogWithSelect extends Dialog implements OnClickListener { 

private String _text; 
Button okButton; 
Button cancelButton; 
/** 
* ProgressDialog that will be shown during the loading process 
*/ 
private    ProgressDialog   myDialog; 

public DialogWithSelect getDialog() { 
    return this; 
} 

public String getText() { 
    return this._text; 
} 

public DialogWithSelect(Context context) { 
    super(context); 
    myDialog = new ProgressDialog(this.getContext()); 
    myDialog.setMessage("Exporting file..."); 
    /** 'Window.FEATURE_NO_TITLE' - Used to hide the title */ 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    /** Design the dialog in main.xml file */ 
    setContentView(R.layout.dialog_with_select_box); 

    final Spinner hubSpinner = (Spinner) findViewById(R.id.spinnerSelectFormat); 
    ArrayAdapter adapter = ArrayAdapter.createFromResource(this.getContext(), R.array.spinner , android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    hubSpinner.setAdapter(adapter); 


    okButton = (Button) findViewById(R.id.okButton); 
    cancelButton = (Button) findViewById(R.id.cancelButton); 

     okButton.setOnClickListener(new View.OnClickListener(){ 

      public void onClick(View v) { 
      //whatever 
      } 

     }); 



    cancelButton.setOnClickListener(new View.OnClickListener(){ 

     public void onClick(View v) { 
      //Get the text from the texString to paint on the Canvas 
      getDialog().hide(); 
     } 

    } 
); 


} 

定義上它是將要使用的類的對話框:

final DialogWithSelect dialog = new DialogWithSelect(getContext()); 
dialog.setTitle(R.string.dialogSelectBoxText); 

並在點擊事件中啓動它:

dialog.show(); 
+0

我實際上已經開始工作了!然而這很奇怪。我創建了一個全新的項目來測試這個自定義對話框的事情,它在我的第一次嘗試中工作,因爲我沒有攜帶xml(我從頭開始創建它)。所以我做的是從頭開始生成xml,並且它工作正常。謝謝您的幫助。雖然知道它有什麼問題會是非常有趣的... – aarelovich