2015-04-15 3 views
1

AlertDialog не отображается onclick.AlertDialog не отображается onclick

Когда вы долго нажимаете на кнопку, должно появиться диалоговое окно, которое появляется, но это не так. Доступ к longClicklistener осуществляется, но диалог все равно не появляется.

// Set Reset Button 
    resetBtn = (ImageButton) findViewById(R.id.resetButton); 
    resetBtn.setOnLongClickListener(new Button.OnLongClickListener() { 
     @Override 
     public boolean onLongClick(View v) { 
      AlertDialogWrapper.Builder alertDialog = new AlertDialogWrapper.Builder(MainActivity.this); 
      alertDialog.setPositiveButton(getString(R.string.MAIN_MENU_RESET_ALERT_YES), new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // Write your code here to invoke YES event 
        Toast toast = Toast.makeText(MainActivity.this, getString(R.string.MAIN_MENU_RESET_ALERT_RESULT_YES), Toast.LENGTH_LONG); 
        toast.show(); 
       } 
      });    alertDialog.setNegativeButton(getString(R.string.MAIN_MENU_RESET_ALERT_NO), new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        Toast.makeText(MainActivity.this, getString(R.string.MAIN_MENU_RESET_ALERT_RESULT_NO), Toast.LENGTH_SHORT).show(); 
        dialog.cancel(); 
       } 
      }); 
      // Showing Alert Message 
      return true; 
     } 
    }); 

ответ

1

У вас не хватает alertDialog.show()

// Set Reset Button 
resetBtn = (ImageButton) findViewById(R.id.resetButton); 
resetBtn.setOnLongClickListener(new Button.OnLongClickListener() { 
    @Override 
    public boolean onLongClick(View v) { 
     AlertDialogWrapper.Builder alertDialog = new AlertDialogWrapper.Builder(MainActivity.this); 
     alertDialog.setPositiveButton(getString(R.string.MAIN_MENU_RESET_ALERT_YES), new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       // Write your code here to invoke YES event 
       Toast toast = Toast.makeText(MainActivity.this, getString(R.string.MAIN_MENU_RESET_ALERT_RESULT_YES), Toast.LENGTH_LONG); 
       toast.show(); 
      } 
     });     
     alertDialog.setNegativeButton(getString(R.string.MAIN_MENU_RESET_ALERT_NO), new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int which) { 
       Toast.makeText(MainActivity.this, getString(R.string.MAIN_MENU_RESET_ALERT_RESULT_NO), Toast.LENGTH_SHORT).show(); 
       dialog.cancel(); 
      } 
     }); 
     /*** Showing Alert Message ***/ 
     alertDialog.show(); 
     return true; 
    } 
}); 
0
// Set Reset Button 
    resetBtn = (ImageButton) findViewById(R.id.resetButton); 
    resetBtn.setOnLongClickListener(new Button.OnLongClickListener() { 
     @Override 
     public boolean onLongClick(View v) { 
      AlertDialogWrapper.Builder alertDialog = new AlertDialogWrapper.Builder(MainActivity.this); 
      alertDialog.setPositiveButton(getString(R.string.MAIN_MENU_RESET_ALERT_YES), new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // Write your code here to invoke YES event 
        Toast toast = Toast.makeText(MainActivity.this, getString(R.string.MAIN_MENU_RESET_ALERT_RESULT_YES), Toast.LENGTH_LONG); 
        toast.show(); 
       } 
      });    alertDialog.setNegativeButton(getString(R.string.MAIN_MENU_RESET_ALERT_NO), new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        Toast.makeText(MainActivity.this, getString(R.string.MAIN_MENU_RESET_ALERT_RESULT_NO), Toast.LENGTH_SHORT).show(); 
        dialog.cancel(); 
       } 
      }); 
alertDialog.show(); 
      // Showing Alert Message 
      return true; 
     } 
    }); 

вам нужно вызвать шоу()

0

Просто добавьте

alertDialog.show(); 

до return true.

То есть,

// Showing Alert Message 
    alertDialog.show(); // code to show alert message [Missed] 
    return true; 
0

Вы пропустили основную линию кода для отображения диалогового окна ... просто добавить ниже линии

// Showing Alert Message 
    alertDialog.show(); 
    return true; 
0

Измените ваш AlertDialogWrapper.Builder на AlertDialog.Builder. А до return true; звоните alertDialog.create(); и alertDialog.show().

0

У вас не хватает alertDialog.show() и смотрите пример для оповещения о диалоговом окне на длинных нажмите

SingleButtton.setOnLongClickListener(new OnLongClickListener() { 

     @Override 
     public boolean onLongClick(View v) { 
     // Creating alert Dialog with one Button 

     AlertDialog alertDialog = new 
     AlertDialog.Builder(AlertDialogActivity.this).create(); 

     // Setting Dialog Title 
     alertDialog.setTitle("Alert Dialog"); 

     // Setting Dialog Message 
     alertDialog.setMessage("Welcome to Android Application"); 

     // Setting Icon to Dialog 
     alertDialog.setIcon(R.drawable.tick); 

     // Setting OK Button 
     alertDialog.setButton("OK", new 
     DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog,int which) 
        { 
         // Write your code here to execute after dialog closed 
        Toast.makeText(getApplicationContext(),"You clicked on OK", Toast.LENGTH_SHORT).show(); 
        } 
       }); 

     // Showing Alert Message 
     alertDialog.show(); 
     return true; 

    } 
}); 


btnAlertTwoBtns.setOnLongClickListener(new OnLongClickListener() { 

     @Override 
     public boolean onLongClick(View v) { 
     // Creating alert Dialog with two Buttons 

     AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this); 

     // Setting Dialog Title 
     alertDialog.setTitle("Confirm Delete..."); 

     // Setting Dialog Message 
     alertDialog.setMessage("Are you sure you want delete this?"); 

     // Setting Icon to Dialog 
     alertDialog.setIcon(R.drawable.delete); 

     // Setting Positive "Yes" Button 
     alertDialog.setPositiveButton("YES", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int which) { 
         // Write your code here to execute after dialog 
         Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show(); 
        } 
       }); 
     // Setting Negative "NO" Button 
     alertDialog.setNegativeButton("NO", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int which) { 
         // Write your code here to execute after dialog 
         Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show(); 
         dialog.cancel(); 
        } 
       }); 

     // Showing Alert Message 
     alertDialog.show(); 
     return true; 

    } 
}); 

btnAlertThreeBtns.setOnLongClickListener(new OnLongClickListener() { 

     @Override 
     public boolean onLongClick(View v) { 
     // Creating alert Dialog with three Buttons 

     AlertDialog.Builder alertDialog = new AlertDialog.Builder(
       AlertDialogActivity.this); 

     // Setting Dialog Title 
     alertDialog.setTitle("Save File..."); 

     // Setting Dialog Message 
     alertDialog.setMessage("Do you want to save this file?"); 

     // Setting Icon to Dialog 
     alertDialog.setIcon(R.drawable.save); 

     // Setting Positive Yes Button 
     alertDialog.setPositiveButton("YES", 
       new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog, 
          int which) { 
         // User pressed Cancel button. Write Logic Here 
         Toast.makeText(getApplicationContext(), 
           "You clicked on YES", 
           Toast.LENGTH_SHORT).show(); 
        } 
       }); 
     // Setting Negative No Button... Neutral means in between yes and cancel button 
     alertDialog.setNeutralButton("NO", 
       new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog, 
          int which) { 
         // User pressed No button. Write Logic Here 
         Toast.makeText(getApplicationContext(), 
           "You clicked on NO", Toast.LENGTH_SHORT) 
           .show(); 
        } 
       }); 
     // Setting Positive "Cancel" Button 
     alertDialog.setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 

        public void onClick(DialogInterface dialog, 
          int which) { 
         // User pressed Cancel button. Write Logic Here 
         Toast.makeText(getApplicationContext(), 
           "You clicked on Cancel", 
           Toast.LENGTH_SHORT).show(); 
        } 
       }); 
     // Showing Alert Message 
     alertDialog.show(); 
     return true; 

    } 
}); 
0
You forget to do this...... 

AlertDialog aD = alertDialog.create(); 

    try { 
     aD.show(); 
    } catch (Exception e) { 

     e.printStackTrace(); 
    } 
Смежные вопросы