2011-01-28 96 views
1

我想顯示其結構已在XML文件中定義的自定義警報對話框。這是我使用的代碼。未顯示自定義警報對話框

AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setView(findViewById(R.layout.inputform)); 
builder.show(); 

單擊按鈕時會觸發。但是,該窗口未顯示。我在屏幕頂部獲得了這個透明的灰色圖層,無法點擊任何東西。不過,使用setMessage或顯示覆選框的一些基本代碼很適用。任何人都可以指出我做錯了什麼?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 

<TableLayout android:id="@+id/TableLayout01" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 

<TableRow android:id="@+id/TableRow01" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:text="Room name" android:id="@+id/TextView01" 
     android:layout_width="fill_parent" android:layout_height="wrap_content"/> 
    <EditText android:hint="Enter room name" android:id="@+id/EditText01" 
     android:layout_width="0dp" android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
</TableRow> 

<TableRow android:id="@+id/TableRow02" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView android:text="Point id" android:id="@+id/TextView02" 
     android:layout_width="fill_parent" android:layout_height="wrap_content"/> 
    <EditText android:hint="Enter point id" android:id="@+id/EditText02" 
     android:layout_width="0dp" android:layout_height="wrap_content" 
     android:layout_weight="1"/> 
</TableRow> 

<TableRow android:id="@+id/TableRow03" android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <Button android:text="OK" android:id="@+id/btnOK" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
</TableRow> 

</TableLayout> 
</LinearLayout> 
+0

你的對話框的XML代碼? – WarrenFaith 2011-01-28 12:48:18

回答

6

這是因爲findViewById用於查找當前活動佈局中的特定視圖,而不是加載整個佈局。

您可以使用@Matt's答案或者你可以用LayoutInflater這樣膨脹的佈局:

LayoutInflater li = getLayoutInflater(); 
View v = li.inflate(R.layout.inputform); 
AlertDialog.Builder builder = new AlertDialog.Builder(this); 
builder.setView(v); 
builder.show(); 

(沒試過,但我認爲它應該工作)。

4

試試這個:

Dialog dialog = new Dialog(this); 
dialog.setContentView(R.layout.inputform); 
dialog.show(); 
+1

它的工作原理!但我正在等待接受你的答案,因爲我想嘗試maid450的解決方案。順便說一下,使用Dialog和AlertDialog有什麼區別?超類是否有所作爲? – springrolls 2011-01-28 13:08:01

+1

+1替代答案。謝謝! – springrolls 2011-01-28 13:14:42

1

嘗試增加builder.create();在builder.show()之前。

+0

不,它不起作用。我正在嘗試其他建議。不管怎麼說,還是要謝謝你! – springrolls 2011-01-28 12:59:55

1

您可以使用下面的類

​​
1

代表對話簡單的方式...

AlertDialog dialog ; 

AlertDialog.Builder builder=new AlertDialog.Builder(this); 

builder.setTitle("Your Title Here"); 

builder.setView(view);//view is your inflate xml layout 

dialog = builder.create(); 

    dialog.show(); 


dialog.dismiss();//whenever you want to dismiss ,put this line 

簡單的對話here