简体   繁体   中英

Show an AlertDialog when button is clicked

I am trying to view a dialog box with radio buttons but when I clicked on the button alert dialog box never appears. Below is the code. Please let me know if there is any alternate solution to this. This sample was taken from API Demos.

public class MainPage extends Activity{
Button start;
private static final int DIALOG_SINGLE_CHOICE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainpage);
    start = (Button) findViewById(R.id.start);
    start.setBackgroundResource(R.drawable.read);
    start.setOnClickListener(new View.OnClickListener() {
        @SuppressWarnings("deprecation")
        public void onClick(View v) {

            showDialog(DIALOG_SINGLE_CHOICE );

        }
    });

}

@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
    id = DIALOG_SINGLE_CHOICE;
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Choose your option");
    builder.setSingleChoiceItems(R.array.Baani, 0, new OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    })
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked Yes so do some stuff */
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked No so do some stuff */
                }
            })
           .create();
    return super.onCreateDialog(id);
        }   
  }
}

Create this function and use showAskDialog() function in your click event

private void showAskDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("DialogTitle").setMessage("DialogMessage").setCancelable(true).setSingleChoiceItems(R.array.Baani, 0, new OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                  // TODO Auto-generated method stub

                }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    dialog.cancel();
                }
            }).setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    dialog.cancel();
                }
            });
    // before @TronicZomB advice
    //AlertDialog alert = builder.create();
    //alert.show();
    // after @TronicZomB advice
    builder.show();
}

You need to replace return super.onCreateDialog(id); with return builder.show(); . .create() will create an alert dialog from the builder but it does not display that dialog. .show() will create the dialog from the builder and show it on the screen.

I also think that the int id might be unnecessary. You could try the following code and it should work for you:

public class MainPage extends Activity{
Button start;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.mainpage);
start = (Button) findViewById(R.id.start);
start.setBackgroundResource(R.drawable.read);
start.setOnClickListener(new View.OnClickListener() {
    @SuppressWarnings("deprecation")
    public void onClick(View v) {

        showDialog(DIALOG_SINGLE_CHOICE );

    }
});

}

@Override
public Dialog onCreateDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Choose your option");
    builder.setSingleChoiceItems(R.array.Baani, 0, new OnClickListener() {

        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    })
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked Yes so do some stuff */
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked No so do some stuff */
                }
            });
        return builder.show();
        }   
  }
}

You're doing it wrong inside onCreateDialog() .

First, you shouldn't do id = DIALOG_SINGLE_COICE but test id value if it equals to DIALOG_SINGLE_CHOICE . * If yes, then create the Dialog with AlertDialog.Builder and return builder.create() . * If no, return super.onCreateDialog(id) .

But this method is deprecated, you should use DialogFragment instead.

public class MainActivity extends Activity implements OnClickListener{
private Button button;
private static final int DIALOG_SINGLE_CHOICE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button  = (Button) findViewById(R.id.button);
    button.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.button:
        showDialog(DIALOG_SINGLE_CHOICE);
        break;

    default:
        break;
    }
}

@Override
@Deprecated
protected Dialog onCreateDialog(int id) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Choose your option");
    builder.setSingleChoiceItems(R.array.Baani, 0, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
        }

    }).setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            /* User clicked Yes so do some stuff */
        }
    }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            /* User clicked No so do some stuff */
        }
    }).create().show();
    return super.onCreateDialog(id);
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM