This article will introduce the common methodsAlertDialog Api
And its common useAlertDialog
Examples
Common API
common method
Method definition | Method statement |
---|---|
setIcon(int iconId) | Set the icon for the dialog box |
setCancelable(boolean cancelable) | Set whether the dialog box can be cancelled |
setTitle(CharSequence title) | Set the title of the dialog box |
setMessage(CharSequence message) | Set the main content of the dialog box |
setAdapter(ListAdapter adapter, OnClickListener listener) | Set an adapter |
setSingleChoiceItems() | Set radio |
setMultiChoiceItems() |
Set multiple choice |
Set wide full screen
Window dialogWindow = alert.getWindow();
WindowManager.LayoutParams p = dialogWindow.getAttributes();
p.width = WindowManager.LayoutParams.MATCH_PARENT;
dialogWindow.setAttributes(p);
When alertdialog is set to wide full screen, the display has the margin attribute
alert.getWindow().setBackgroundDrawable(null);
Set center
alert.getWindow().setGravity(Gravity.CENTER);
How to click the blank space in alterdialog
alert.setCanceledOnTouchOutside(false);
Examples
Default normal dialog box
AlertDialog dialog = new AlertDialog
.Builder(this)
. settitle ("normal dialog box")
.setIcon(R.mipmap.ic_launcher)
. setnegativebutton ("Cancel", null)
. setpositivebutton ("OK", new) DialogInterface.OnClickListener () {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText (getapplicationcontext (), "OK,", Toast.LENGTH_ SHORT).show();
}
}). Setneutralbutton ("default", null)
. setMessage ("are you sure you want to delete? ").create();
dialog.show();
displayshowItemdialogueframe
Final charsequence [] items = {"Beijing", "Shanghai", "Guangzhou"};
AlertDialog dialog = new AlertDialog
.Builder(this)
. settitle ("show Item dialog box")
.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show();
}
}).create();
dialog.show();
Radio list dialog setsinglechoiceitems
Final charsequence [] items = {"Beijing", "Shanghai", "Guangzhou"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle ("please select the following cities");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
CharSequence sequence = items[which];
Toast.makeText(getApplicationContext(), "select " + sequence, Toast.LENGTH_SHORT).show();
}
});
AlertDialog dialog = builder.create();
dialog.show();
Multiple selection dialog setmultichoiceitems
Final charsequence [] items = {"Beijing", "Shanghai", "Guangzhou"};
AlertDialog dialog = new AlertDialog.Builder (this). Settitle ("multiple choice dialog"). Seticon (Android. R drawable.sym_ def_ app_ icon)
. setnegativebutton ("Cancel", null). Setpositivebutton ("OK", null)
.setMultiChoiceItems(items, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(getApplicationContext(), "select " + items[which], Toast.LENGTH_SHORT).show();
}
}).create();
dialog.show();
Custom adapter
Final string [] items = {"Beijing", "Shanghai", "Guangzhou"};
ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
AlertDialog dialog = new AlertDialog.Builder (this). Settitle ("custom adapter"). Seticon (Android. R drawable.sym_ def_ app_ icon)
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "select " + items[which], Toast.LENGTH_SHORT).show();
}
}).create();
dialog.show();
Custom view
String name = custom view;
View view = LayoutInflater.from(this).inflate(android.R.layout.simple_list_item_1, null);
((TextView)view.findViewById(android.R.id.text1)).setText(name);
AlertDialog dialog = new AlertDialog
.Builder(this)
.setTitle(name)
.setIcon(android.R.drawable.sym_def_app_icon)
.setView(view)
.create();
dialog.show();