Android:AlertDialog没有显示出来

时间:2015-10-14 05:33:23

标签: android android-layout android-alertdialog

我想要一个警报对话框(一个从用户那里获得一些输入的表单),以便在活动中按下时显示,例如

@Override
public void onBackPressed() {
    showFormDialog();
}

private void showFormDialog() {
    //Preparing views
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.dialog_form, (ViewGroup) findViewById(R.id.llid));
    //layout_root should be the name of the "top-level" layout node in the dialog_layout.xml file.
    final EditText txtFB = (EditText) layout.findViewById(R.id.txtFB);

    //Building dialog
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setView(layout);
    builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
            //save info where you want it
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });
    AlertDialog dialog = builder.create();
}

dialog_form.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/llid"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <EditText
            android:id="@+id/txtFB"
            android:layout_width="250dp"
            android:layout_height="200dp"
            android:text="hello type something here"/>
    </LinearLayout> </LinearLayout>

我可以看到showFormDialog代码是在背面按下时执行的,但是没有出现AlertDialog!

3 个答案:

答案 0 :(得分:2)

您错过了致电from Tkinter import * def search(): #FAULTY BIT HERE!! e1_var.get() #and just to test... print e1_var root=Tk() root.wm_title("SearchPy") w = 500 # width for the Tk root h = 500 # height for the Tk root # get screen width and height ws = root.winfo_screenwidth() # width of the screen hs = root.winfo_screenheight() # height of the screen # calculate x and y coordinates for the Tk root window x = (ws/2) - (w/2) y = (hs/2) - (h/2) # set the dimensions of the screen # and where it is placed root.geometry('%dx%d+%d+%d' % (w, h, x, y)) #main buttons and entrys to get url. e1_var=StringVar #e1_var has been set a StringVar which is a StringVar instance? l1=Label(root, text="Search").grid(column=0, row=1) #no explanation needed!! #text entry e1=Entry(root, textvariable=e1_var).grid(column=1, row=1) #search button b1=Button(root, text="Go!", command=search()).grid(row=2) root.mainloop()

.show()

dialog.show(); 导致使用提供给此构建器的参数创建 AlertDialog 并立即显示该对话框。

答案 1 :(得分:1)

只需添加此

即可
dialog.show();

创造它是不够的。你应该展示它。

答案 2 :(得分:1)

添加dialog.show()

AlertDialog dialog = builder.create();
dialog.show();