2017-02-20 53 views
0

我的點擊操作欄圖標我想要顯示對話框。 (即)我正在使用自定義對話框。 對話框我創建xml佈局。 我試着在下面給出的程序...當我點擊操作欄圖標時,我想要顯示自定義對話框(例如)我們的對話框xml我的對話框

其實我想在點擊圖標的時候顯示自定義對話框(自定義)。

switch (item.getItemId()) { 
    case R.id.icon: 
     final Dialog dialog = new Dialog(context); 
     dialog.setContentView(R.layout.dailog); 
     dialog.setTitle("Custom Dialog Example"); 

     Button dialogButtonCancel = (Button) dialog.findViewById(R.id.cancel); 
     Button dialogButtonOk = (Button) dialog.findViewById(R.id.ok); 
     // Click cancel to dismiss android custom dialog box 
     dialogButtonCancel.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       dialog.dismiss(); 
      } 
     }); 
    } 
    return true; 
} 

當運行應用程序有沒有輸出

回答

0

你必須調用dialog.show()顯示對話框;

另外,您不能在switch中使用return,您需要使用break

編輯:

switch (item.getItemId()) { 
case R.id.icon: 
    final Dialog dialog = new Dialog(context); 
    dialog.setContentView(R.layout.dailog); 
    dialog.setTitle("Custom Dialog Example"); 

    Button dialogButtonCancel = (Button) dialog.findViewById(R.id.cancel); 
    Button dialogButtonOk = (Button) dialog.findViewById(R.id.ok); 
    // Click cancel to dismiss android custom dialog box 
    dialogButtonCancel.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      dialog.dismiss(); 
     } 
    }); 
    dialog.show(); 
    break; 
} 

編輯2:另外,您可以使用圖書館Material Dialogs,它真的很容易使用。

+0

嗨,henrique:我試過}); dialog.show(); 休息; } return true; } –

+0

但是沒有進展 –

+0

返回外面的開關是不是?所以它應該沒關係,我誤解它是在第一次。 –

相關問題