Dialog.setnegativebutton cancel new dialoginterface.onclicklistener get strings
Here is an example of how you can set a negative button with the text "Cancel" in a Dialog
and set an OnClickListener
to handle the button click:
Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.your_dialog_layout);
// Set the negative button with the text "Cancel"
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Handle the cancel button click
dialog.dismiss();
}
});
// Set the positive button with the text "OK"
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// Handle the OK button click
dialog.dismiss();
}
});
// Show the dialog
dialog.show();
In this example, we create a Dialog
and set its content view to a layout file (R.layout.your_dialog_layout
). We then set the negative button with the text "Cancel" using the setNegativeButton
method, and set an OnClickListener
to handle the button click. In this case, we simply dismiss the dialog when the cancel button is clicked.
We also set a positive button with the text "OK" using the setPositiveButton
method, and set an OnClickListener
to handle the button click. Again, we simply dismiss the dialog when the OK button is clicked.
Finally, we show the dialog using the show
method.
Note that you can customize the appearance and behavior of the buttons by using the various methods available on the Dialog
class, such as setButtonTextColor
and setButtonBackground
.