2011-01-28 100 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