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();
}
}
如果按钮文本过长,超出水平空间,则按钮将自动在三个垂直列中布置。
在上面的示例中,OnClickListener
是null
。 您可以替换null
以使用侦听器在用户点击按钮时执行某些操作。例如:
builder.setPositiveButton("Launch missile", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// do something like...
launchMissile();
}
});
您可以创建更多种类的对话框。请参阅文档以获取帮助。
由于AlertDialog
仅支持三个按钮,因此这是具有列表的对话框示例。
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();
}
}
请参考此答案,查看类似单选按钮列表和复选框列表的示例。
DialogFragment
的类中,以便轻松重用对话框。(有关帮助,请参见此处)。这些示例使用支持库来支持API 11之前的版本。因此,导入应为
import android.support.v7.app.AlertDialog;
为了简洁起见,上面的示例中省略了onCreate
方法。那里没有什么特别之处。
showAlertDialogButtonClicked(View view)
中的view
。那只是来自按钮的onClick()
方法的Button。您可以忽略它。对话框根本不使用它。对话框只需要上下文,这种情况下是this
,也就是Activity。 - Suragchandroid:alertDialogTheme
中为我的android:buttonBarButtonStyle
添加了一个<item name="android:padding">10dp</item>
。看起来可以工作了。 - testing