2015-10-14 42 views
-2

我想要一個警報對話框(一個窗體從用戶接受一些輸入)在活動中按下時顯示,例如。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顯示出來!

回答

2

你錯過來電.show()

dialog.show(); 

.show()原因與提供給該生成器的參數創建一個AlertDialog並立即顯示該對話框。

+1

哎呀...錯過了! – Jasper

1

只需添加這

dialog.show(); 

創建它是不夠的。你應該展示它。

1

添加dialog.show()

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