下面列出了android.app.AlertDialog.Builder#setMessage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static AlertDialog showAlert(final Context context, final String msg, final String title, final String yes, final String no, final DialogInterface.OnClickListener lOk,
final DialogInterface.OnClickListener lCancel) {
if (context instanceof Activity && ((Activity) context).isFinishing()) {
return null;
}
final Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.ic_dialog_alert);
builder.setTitle(title);
builder.setMessage(msg);
builder.setPositiveButton(yes, lOk);
builder.setNegativeButton(no, lCancel);
// builder.setCancelable(false);
final AlertDialog alert = builder.create();
alert.show();
return alert;
}
/**
* Show a model dialog box. The <code>android.app.AlertDialog</code> object is returned so that
* you can specify an OnDismissListener (or other listeners) if required.
* <b>Note:</b> show() is already called on the AlertDialog being returned.
*
* @param context The current Context or Activity that this method is called from.
* @param message Message to display in the dialog.
* @return AlertDialog that is being displayed.
*/
public static AlertDialog quickDialog(final Activity context, final String message) {
final SpannableString s = new SpannableString(message); //Make links clickable
Linkify.addLinks(s, Linkify.ALL);
final Builder builder = new AlertDialog.Builder(context);
builder.setMessage(s);
builder.setPositiveButton(android.R.string.ok, closeDialogListener());
AlertDialog dialog = builder.create();
if(!context.isFinishing()) {
dialog.show();
final TextView textView = (TextView) dialog.findViewById(android.R.id.message);
if (textView != null) {
textView.setMovementMethod(LinkMovementMethod.getInstance()); //Make links clickable
}
}
return dialog;
}
/**
* Prompt user whether to proceed or not, if so, execute runnable
*
* @param context
* @param titleResId
* @param msgResId
* @param onConfirm
*/
public static void confirm(Context context, String logTag, int titleResId, int msgResId,
int OkResId, int cancelResId, final Runnable onConfirm) {
AlertDialog dialog = null;
try {
Builder b = new Builder(context);
b.setCancelable(true);
if (titleResId >= 0) b.setTitle(titleResId);
if (msgResId >= 0) b.setMessage(msgResId);
if (cancelResId >= 0) b.setNegativeButton(cancelResId, null);
if (onConfirm != null && OkResId >= 0) b.setPositiveButton(OkResId, new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
onConfirm.run();
}
});
b.create().show();
} catch (Exception e) {
if (logTag != null) Utils.handleException(logTag, context, e);
}
}
public static AlertDialog showAlert(final Context context, final int msgId, final int titleId) {
if (context instanceof Activity && ((Activity) context).isFinishing()) {
return null;
}
final Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.ic_dialog_alert);
builder.setTitle(titleId);
builder.setMessage(msgId);
builder.setPositiveButton(R.string.app_ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(final DialogInterface dialog, final int which) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
return alert;
}
public static AlertDialog showAlert(final Context context, final int msgId, final int titleId, final DialogInterface.OnClickListener l) {
if (context instanceof Activity && ((Activity) context).isFinishing()) {
return null;
}
final Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.ic_dialog_alert);
builder.setTitle(titleId);
builder.setMessage(msgId);
builder.setPositiveButton(R.string.app_ok, l);
// builder.setCancelable(false);
final AlertDialog alert = builder.create();
alert.show();
return alert;
}
/**
* Display our about dialog.
*/
private void showAbout() {
// Get package information
String version;
try {
PackageInfo pkgInfo;
pkgInfo = getPackageManager().getPackageInfo(this.getPackageName(), 0);
version = pkgInfo.versionName;
} catch (NameNotFoundException e) {
version = getString(R.string.unknown);
}
StringBuilder message = new StringBuilder();
message.append(getString(R.string.about_desc)).append("<br /><br />");
message.append(getString(R.string.copyright)).append("<br /><br />");
message.append(getString(R.string.about_version)).append(" ").append(version);
message.append("<br /><br />");
message.append(getString(R.string.about_license)).append("<br /><br />");
message.append(getString(R.string.about_website));
Builder about = new AlertDialog.Builder(this);
about.setTitle(R.string.about_title);
about.setIcon(R.drawable.icon);
about.setMessage(Html.fromHtml(message.toString()));
about.setPositiveButton(R.string.ok, null);
about.show();
}
public void onClick(View v) {
Builder help = new AlertDialog.Builder(v.getContext());
help.setIcon(android.R.drawable.ic_menu_help);
help.setTitle(R.string.help);
help.setMessage(Html.fromHtml(getString(R.string.help_dlgfilterinput)));
help.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
help.show();
}
public void onClick(View v) {
Builder help = new AlertDialog.Builder(v.getContext());
help.setIcon(android.R.drawable.ic_menu_help);
help.setTitle(R.string.help);
help.setMessage(Html.fromHtml(getString(R.string.help_dlgactioninput)));
help.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
help.show();
}
public static AlertDialog okCancelDialog(Context context, String title,
String message, DialogInterface.OnClickListener okCancel) {
Builder dialogBuilder = new Builder(context);
dialogBuilder.setPositiveButton(context.getResources().getString(
R.string.general_ok), okCancel);
dialogBuilder.setTitle(title);
dialogBuilder.setMessage(message);
dialogBuilder.setNegativeButton(context.getResources().getString(
R.string.general_cancel), okCancel);
return dialogBuilder.create();
}
public void openDialogMessage(int title,int message){
final Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setMessage(message);
builder.setCancelable(true);
builder.setPositiveButton(R.string.ok_btn, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
builder.create().dismiss();
}
});
builder.show();
}
public void openDialogMessage(int title,int message,boolean isCancelable){
final Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setMessage(message);
builder.setCancelable(isCancelable);
builder.setPositiveButton(R.string.ok_btn, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
builder.create().dismiss();
finish();
}
});
builder.show();
}
public static AlertDialog getSelectNewOwnerDialog(final Activity activity, final ArrayList<String> usernameList) {
CharSequence[] listCharArray = usernameList.toArray(new CharSequence[usernameList.size()]);
Builder dialogBuilder = new AlertDialog.Builder(activity)
.setNegativeButton(R.string.cancel, null);
if (usernameList.size() > 0) {
dialogBuilder.setSingleChoiceItems(listCharArray, 0, null)
.setPositiveButton(R.string.transfer, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ListView listView = ((AlertDialog) dialog).getListView();
Object checkedItem = listView.getAdapter().getItem(listView
.getCheckedItemPosition());
String selectedUsername = checkedItem.toString();
if (activity instanceof SharingActivity) {
TransferOwnershipTask.launch(activity, SharingActivity
.evercamCamera.getCameraId(), selectedUsername);
}
}
}).setTitle(R.string.transfer_select_title);
} else {
dialogBuilder.setMessage(R.string.msg_share_before_transfer);
}
return dialogBuilder.create();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Builder alertDialog = new AlertDialog.Builder(getActivity());
alertDialog.setTitle("Alert!");
alertDialog.setMessage("This is a notication dialog");
return alertDialog.create();
}
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
if (savedInstanceState != null) {
mTitleId = savedInstanceState.getInt(DialogKeys.TITLE_ID);
mMessageId = savedInstanceState.getInt(DialogKeys.MESSAGE_ID);
mNegativeLabelId = savedInstanceState
.getInt(DialogKeys.NEGATIVE_LABEL_ID);
mNeutralLabelId = savedInstanceState
.getInt(DialogKeys.NEUTRAL_LABEL_ID);
mPositiveLabelId = savedInstanceState
.getInt(DialogKeys.POSITIVE_LABEL_ID);
isCancellable = savedInstanceState
.getBoolean(DialogKeys.CANCELLABLE);
mIconId = savedInstanceState.getInt(DialogKeys.ICON_ID);
mTheme = savedInstanceState.getInt(DialogKeys.THEME);
mMessageParams = savedInstanceState
.getStringArray(DialogKeys.MESSAGE_PARAMS);
}
final Builder builder = new Builder(getActivity(), mTheme);
if ((mTitleId == 0) && (mMessageId == 0)) {
throw new IllegalArgumentException("No Title and no message");
}
if (mIconId != 0) {
builder.setIcon(mIconId);
}
if (mTitleId != 0) {
builder.setTitle(mTitleId);
}
if (mMessageId != 0) {
if ((mMessageParams != null) && (mMessageParams.length > 0)) {
builder.setMessage(getActivity()
.getString(mMessageId, (Object[]) mMessageParams));
} else {
builder.setMessage(mMessageId);
}
}
if (mPositiveLabelId != 0) {
builder.setPositiveButton(mPositiveLabelId, mClickListener);
}
if (mNegativeLabelId != 0) {
builder.setNegativeButton(mNegativeLabelId, mClickListener);
}
if (mNeutralLabelId != 0) {
builder.setNeutralButton(mNeutralLabelId, mClickListener);
}
builder.setCancelable(isCancellable);
setCancelable(isCancellable);
return builder.create();
}
public void genericOkDialog(int title, int message, DialogInterface.OnClickListener okListener) {
if (!isFinishing()) {
Builder builder = new Builder(this);
// Title
if (title != -1) {
builder.setTitle(title);
}
// Message
builder.setMessage(message);
// Ok
builder.setPositiveButton(R.string.ok, okListener);
// Create dialog
AlertDialog dialog = builder.create();
// Show dialog
dialog.show();
}
}
/**
* Display a simple alert dialog with the given text and title.
*
* @param context
* Android context in which the dialog should be displayed
* @param title
* Alert dialog title
* @param text
* Alert dialog message
*/
@Deprecated
public static void showAlert(Context context, String title, String text) {
Builder alertBuilder = new Builder(context);
alertBuilder.setTitle(title);
alertBuilder.setMessage(text);
alertBuilder.create().show();
}
/**
* Display a simple alert dialog with the given text and title.
*
* @param context
* Android context in which the dialog should be displayed
* @param title
* Alert dialog title
* @param text
* Alert dialog message
*/
@Deprecated
public static void showAlert(Context context, String title, String text) {
Builder alertBuilder = new Builder(context);
alertBuilder.setTitle(title);
alertBuilder.setMessage(text);
alertBuilder.create().show();
}
/**
* Display a simple alert dialog with the given text and title.
*
* @param context
* Android context in which the dialog should be displayed
* @param title
* Alert dialog title
* @param text
* Alert dialog message
*/
public static void showAlert(Context context, String title, String text) {
Builder alertBuilder = new Builder(context);
alertBuilder.setTitle(title);
alertBuilder.setMessage(text);
alertBuilder.create().show();
}
/**
* Display a simple alert dialog with the given text and title.
*
* @param context
* Android context in which the dialog should be displayed
* @param title
* Alert dialog title
* @param text
* Alert dialog message
*/
@Deprecated
public static void showAlert(Context context, String title, String text) {
Builder alertBuilder = new Builder(context);
alertBuilder.setTitle(title);
alertBuilder.setMessage(text);
alertBuilder.create().show();
}
/**
* Display a simple alert dialog with the given text and title.
*
* @param context
* Android context in which the dialog should be displayed
* @param title
* Alert dialog title
* @param text
* Alert dialog message
*/
@Deprecated
public static void showAlert(Context context, String title, String text) {
Builder alertBuilder = new Builder(context);
alertBuilder.setTitle(title);
alertBuilder.setMessage(text);
alertBuilder.create().show();
}