import android.support.v7.app.AlertDialog;
public class MainActivity extends AppCompatActivity {
public void showAlertDialogButtonClicked(View view) {
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("My title");
builder.setMessage("This is my message.");
// add a button
builder.setPositiveButton("OK", null);
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}
}
public class MainActivity extends AppCompatActivity {
public void showAlertDialogButtonClicked(View view) {
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("AlertDialog");
builder.setMessage("Would you like to continue learning how to use Android alerts?");
// add the buttons
builder.setPositiveButton("Continue", null);
builder.setNegativeButton("Cancel", null);
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}
}
public class MainActivity extends AppCompatActivity {
public void showAlertDialogButtonClicked(View view) {
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Notice");
builder.setMessage("Launching this missile will destroy the entire universe. Is this what you intended to do?");
// add the buttons
builder.setPositiveButton("Launch missile", null);
builder.setNeutralButton("Remind me later", null);
builder.setNegativeButton("Cancel", null);
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}
}
If the button text it too long to all fit horizontally, then it will automatically get laid out in a vertical column of three buttons.
The OnClickListener
was null
in the above examples. You can replace null
with a listener to do something when the user taps a button. For example:
builder.setPositiveButton("Launch missile", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do something like...
launchMissile();
}
});
There are many more varieties of dialogs that you can make. See the documentation for help with this.
Since only three buttons are supported in an AlertDialog
, here is an example of a dialog with a list.
public class MainActivity extends AppCompatActivity {
public void showAlertDialogButtonClicked(View view) {
// setup the alert builder
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose an animal");
// add a list
String[] animals = {"horse", "cow", "camel", "sheep", "goat"};
builder.setItems(animals, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0: // horse
case 1: // cow
case 2: // camel
case 3: // sheep
case 4: // goat
}
}
});
// create and show the alert dialog
AlertDialog dialog = builder.create();
dialog.show();
}
}
See this answer for similar examples of a radio button list and a checkbox list.
DialogFragment
以方便对话框的重用。(请参阅此以获得帮助。)这些示例使用支持库来支持API 11之前的版本。因此,导入应为
import android.support.v7.app.AlertDialog;
onCreate
为了简洁起见,我在上面的示例中省略了该方法。那里没有什么特别的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句