custom alert dialog not getting displayed

2019-08-09 07:22发布

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();

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>

5条回答
兄弟一词,经得起流年.
2楼-- · 2019-08-09 07:44

Try adding builder.create(); before builder.show().

查看更多
别忘想泡老子
3楼-- · 2019-08-09 07:47

Simple way to represent dialog...

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

simple dialog here

查看更多
爷、活的狠高调
4楼-- · 2019-08-09 07:48

try this:

Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.inputform);
dialog.show();
查看更多
相关推荐>>
5楼-- · 2019-08-09 07:56

You can use the following class

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;
    }
            }
查看更多
Lonely孤独者°
6楼-- · 2019-08-09 08:08

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

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

查看更多
登录 后发表回答