可绘制的警报对话框

时间:2013-01-04 06:42:07

标签: android android-alertdialog android-menu

我已经有onOptionsItemSelected(MenuItem item) 提交按钮。

switch(item.getItemId()){
case R.id.submit:

我想要一个alertDialog。我怎么能这样做此提交是res> drawable文件夹中图片的ID。 那么如何向此添加警告对话框?

总结:我想要点击提交按钮的确认。在后,我想调用submitDefects();函数。

1 个答案:

答案 0 :(得分:1)

public boolean onOptionsItemSelected (MenuItem item)
 {
 switch(item.getItemId()){
 case R.id.submit:
  AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);

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

    // 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 invoke YES event
        Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
                     submitDefects();
        }
    });

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

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