-4

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

在里面

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

我想要一个警报对话框。我该怎么做。此提交是 res>drawable 文件夹中图像的 id。那么如何为此添加警报对话框呢?

总结:我想要确认单击的提交按钮。如果,我想调用该submitDefects();函数。

4

1 回答 1

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;
        }
    }
于 2013-01-04T06:57:57.120 回答