【问题标题】:custom alert dialog not getting displayed自定义警报对话框未显示
【发布时间】:2011-06-17 05:34:46
【问题描述】:

我想显示一个自定义警报对话框,其结构已在 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>

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

    这是因为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();
    

    (没试过,但我认为应该可以)。

    【讨论】:

    • 我之前试过了,还是不行。刚刚意识到我在提供 parentView 时犯了错误。谢谢!接受这个作为完整性的答案。
    【解决方案2】:

    试试这个:

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

    【讨论】:

    • 有效!但我正在等待接受您的回答,因为我想尝试 maid450 的解决方案。顺便说一句,使用 Dialog 和 AlertDialog 有什么区别?成为超类有什么不同吗?
    【解决方案3】:

    尝试添加 builder.create();在 builder.show() 之前。

    【讨论】:

    • 不,它不起作用。我正在尝试其他建议。还是谢谢!
    【解决方案4】:

    你可以使用下面的类

    import android.app.Dialog;
    import android.content.Context;
    
    public class CustomDialog extends Dialog {
    
    
        public CustomDialog(Context context) {
            super(context);
    
            }
    
    
        public static CustomDialog show(Context context, CharSequence title,
                CharSequence message) {
            return show(context, title, message, false);
        }
    
        public static CustomDialog show(Context context, CharSequence title,
                CharSequence message, boolean indeterminate) {
            return show(context, title, message, indeterminate, false, null);
        }
    
        public static CustomDialog show(Context context, CharSequence title,
                CharSequence message, boolean indeterminate, boolean cancelable) {
            return show(context, title, message, indeterminate, cancelable, null);
        }
    
        public static CustomDialog show(Context context, CharSequence title,
                CharSequence message, boolean indeterminate,
                boolean cancelable, OnCancelListener cancelListener) {
            CustomDialog dialog = new CustomDialog(context);
            dialog.setTitle(null);
            dialog.setCancelable(cancelable);
            dialog.setOnCancelListener(cancelListener);
    
    
            dialog.setContentView(R.layout.test);
            dialog.show();
    
            return dialog;
        }
                }
    

    【讨论】:

      【解决方案5】:

      表示对话框的简单方法...

      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

      【讨论】:

        猜你喜欢
        • 2020-03-21
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多