遇到多个构造器参数考虑用构建器(Builder)(九)
pacingLeft Spacing between the left edge of the view and
* the dialog frame
* @param viewSpacingTop Spacing between the top edge of the view and
* the dialog frame
* @param viewSpacingRight Spacing between the right edge of the view
* and the dialog frame
* @param viewSpacingBottom Spacing between the bottom edge of the view
* and the dialog frame
* @return This Builder object to allow for chaining of calls to set
* methods
*
*
* This is currently hidden because it seems like people should just
* be able to put padding around the view.
* @hide
*/
public Builder setView(View view, int viewSpacingLeft, int viewSpacingTop,
int viewSpacingRight, int viewSpacingBottom) {
P.mView = view;
P.mViewSpacingSpecified = true;
P.mViewSpacingLeft = viewSpacingLeft;
P.mViewSpacingTop = viewSpacingTop;
P.mViewSpacingRight = viewSpacingRight;
P.mViewSpacingBottom = viewSpacingBottom;
return this;
}
/**
* Sets the Dialog to use the inverse background, regardless of what the
* contents is.
*
* @param useInverseBackground Whether to use the inverse background
*
* @return This Builder object to allow for chaining of calls to set methods
*/
public Builder setInverseBackgroundForced(boolean useInverseBackground) {
P.mForceInverseBackground = useInverseBackground;
return this;
}
/**
* @hide
*/
public Builder setRecycleOnMeasureEnabled(boolean enabled) {
P.mRecycleOnMeasure = enabled;
return this;
}
/**
* Creates a {@link AlertDialog} with the arguments supplied to this builder. It does not
* {@link Dialog#show()} the dialog. This allows the user to do any extra processing
* before displaying the dialog. Use {@link #show()} if you don't have any other processing
* to do and want this to be created and displayed.
*/
public AlertDialog create() {
final AlertDialog dialog = new AlertDialog(P.mContext, mTheme);
P.apply(dialog.mAlert);
dialog.setCancelable(P.mCancelable);
dialog.setOnCancelListener(P.mOnCancelListener);
if (P.mOnKeyListener != null) {
dialog.setOnKeyListener(P.mOnKeyListener);
}
return dialog;
}
/**
* Creates a {@link AlertDialog} with the arguments supplied to this builder and
* {@link Dialog#show()}'s the dialog.
*/
public AlertDialog show() {
AlertDialog dialog = create();
dialog.show();
return dialog;
}
客户端代码用法如下:
[java]
new AlertDialog.Builder(context)
.setTitle("提示")
.setMessage("网络连接未打开")
.setPositiveButton("前往打开",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
Intent intent = new Intent(
android.provider.Settings.ACTION_WIRELESS_SETTINGS);