自定义警报对话框未显示 [英] custom alert dialog not getting displayed

查看:78
本文介绍了自定义警报对话框未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个自定义警报对话框,该对话框的结构已在XML文件中定义.这是我使用的代码.

I want to display a custom alert dialog whose structure has been defined in an XML file. This is the code I used.

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

单击按钮时将触发它.但是,该窗口未显示.我在屏幕顶部得到了透明的灰色层,无法单击任何东西.但是,某些基本代码(如使用setMessage或显示复选框)效果很好.谁能指出我做错了什么地方?

It is fired when a button is clicked. However, the window was not getting displayed. I got this transparent grey layer on top of the screen and could not click on anything. Some basic codes like using setMessage or displaying checkboxes work well, though. Can anyone point out where I do wrong?

<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>

推荐答案

这是因为findViewById用于在当前活动的布局中查找特定视图,而不是用于加载整个布局.

This is because findViewById is for finding a specific view in the current activity´s layout, not for loading a whole layout.

您可以使用 @Matt的答案,或者您也可以可以使用 LayoutInflater 来使布局膨胀:

You can use @Matt's answer or maybe you can inflate the layout with a LayoutInflater like this:

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

(没有尝试过,但我认为它应该可以工作).

(Not tried it but I think it should work).

这篇关于自定义警报对话框未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆