Android 自定义Toast
自定义Toast
其实就是自定义布局文件 感觉利用Dialog或者PopupWindow做也差不多
上图上代码

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button show1 = (Button)findViewById(R.id.show1);
show1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SuperToast superToast = new SuperToast(MainActivity.this);
//设置属性
superToast.setAnimations(SuperToast.Animations.FLYIN); //显示动画
superToast.setDuration(SuperToast.Duration.SHORT); //显示时间 长 中 短
superToast.setBackground(SuperToast.Background.BLACK); //背景颜色
superToast.setTextSize(SuperToast.TextSize.SMALL); //文字大小
superToast.setIcon(R.drawable.icon_load, SuperToast.IconPosition.LEFT); //图片显示
superToast.setText("登录中...");
superToast.setGravity(Gravity.CENTER, 0, 0);
superToast.show();
}
});
Button show2 = (Button)findViewById(R.id.show2);
show2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SuperToast superToast = new SuperToast(MainActivity.this);
//设置属性
superToast.setAnimations(SuperToast.Animations.FLYIN); //显示动画
superToast.setDuration(SuperToast.Duration.SHORT); //显示时间 长 中 短
superToast.setBackground(SuperToast.Background.BLACK); //背景颜色
superToast.setTextSize(SuperToast.TextSize.SMALL); //文字大小
superToast.setText("MeiID或密码错误,请重新输入");
superToast.setGravity(Gravity.CENTER, 0, 0);
superToast.show();
}
});
Button show3 = (Button)findViewById(R.id.show3);
show3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SuperToast superToast = new SuperToast(MainActivity.this);
//设置属性
superToast.setAnimations(SuperToast.Animations.FLYIN); //显示动画
superToast.setDuration(SuperToast.Duration.SHORT); //显示时间 长 中 短
superToast.setBackground(SuperToast.Background.BLACK); //背景颜色
superToast.setTextSize(SuperToast.TextSize.SMALL); //文字大小
superToast.setIcon(R.drawable.icon_ok, SuperToast.IconPosition.LEFT); //图片显示
superToast.setText("重置密码成功");
superToast.setGravity(Gravity.CENTER, 0, 0);
superToast.show();
}
});
Button show4 = (Button)findViewById(R.id.show4);
show4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SuperToast superToast = new SuperToast(MainActivity.this);
//设置属性
superToast.setAnimations(SuperToast.Animations.FLYIN); //显示动画
superToast.setDuration(SuperToast.Duration.SHORT); //显示时间 长 中 短
superToast.setBackground(SuperToast.Background.BLACK); //背景颜色
superToast.setTextSize(SuperToast.TextSize.SMALL); //文字大小
superToast.setText("验证码已发送到您的手机,10分钟内\n有效,请注意查收短信");
superToast.setGravity(Gravity.CENTER, 0, 0);
superToast.show();
}
});
}
}
public class ManagerSuperToast extends Handler {
@SuppressWarnings("UnusedDeclaration")
private static final String TAG = "ManagerSuperToast";
/* Potential messages for the handler to send **/
private static final class Messages {
/* Hexadecimal numbers that represent acronyms for the operation **/
private static final int DISPLAY_SUPERTOAST = 0x445354;
private static final int ADD_SUPERTOAST = 0x415354;
private static final int REMOVE_SUPERTOAST = 0x525354;
}
private static ManagerSuperToast mManagerSuperToast;
private final Queue<SuperToast> mQueue;
/* Private method to create a new list if the manager is being initialized */
private ManagerSuperToast() {
mQueue = new LinkedBlockingQueue<SuperToast>();
}
/* Singleton method to ensure all SuperToasts are passed through the same manager */
protected static synchronized ManagerSuperToast getInstance() {
if (mManagerSuperToast != null) {
return mManagerSuperToast;
} else {
mManagerSuperToast = new ManagerSuperToast();
return mManagerSuperToast;
}
}
/* Add SuperToast to queue and try to show it */
protected void add(SuperToast superToast) {
/* Add SuperToast to queue and try to show it */
mQueue.add(superToast);
this.showNextSuperToast();
}
/* Shows the next SuperToast in the list */
private void showNextSuperToast() {
if (mQueue.isEmpty()) {
/* There is no SuperToast to display next */
return;
}
/* Get next SuperToast in the queue */
final SuperToast superToast = mQueue.peek();
/* Show SuperToast if none are showing (not sure why this works but it does) */
if (!superToast.isShowing()) {
final Message message = obtainMessage(Messages.ADD_SUPERTOAST);
message.obj = superToast;
sendMessage(message);
} else {
sendMessageDelayed(superToast, Messages.DISPLAY_SUPERTOAST,
getDuration(superToast));
}
}
/* Show/dismiss a SuperToast after a specific duration */
private void sendMessageDelayed(SuperToast superToast, final int messageId, final long delay) {
Message message = obtainMessage(messageId);
message.obj = superToast;
sendMessageDelayed(message, delay);
}
/* Get duration and add one second to compensate for show/hide animations */
private long getDuration(SuperToast superToast) {
long duration = superToast.getDuration();
duration += 1000;
return duration;
}
@Override
public void handleMessage(Message message) {
final SuperToast superToast = (SuperToast)
message.obj;
switch (message.what) {
case Messages.DISPLAY_SUPERTOAST:
showNextSuperToast();
break;
case Messages.ADD_SUPERTOAST:
displaySuperToast(superToast);
break;
case Messages.REMOVE_SUPERTOAST:
removeSuperToast(superToast);
break;
default: {
super.handleMessage(message);
break;
}
}
}
/* Displays a SuperToast */
private void displaySuperToast(SuperToast superToast) {
if (superToast.isShowing()) {
/* If the SuperToast is already showing do not show again */
return;
}
final WindowManager windowManager = superToast
.getWindowManager();
final View toastView = superToast.getView();
final WindowManager.LayoutParams params = superToast
.getWindowManagerParams();
if(windowManager != null) {
windowManager.addView(toastView, params);
}
sendMessageDelayed(superToast, Messages.REMOVE_SUPERTOAST,
superToast.getDuration() + 500);
}
/* Hide and remove the SuperToast */
protected void removeSuperToast(SuperToast superToast) {
final WindowManager windowManager = superToast
.getWindowManager();
final View toastView = superToast.getView();
if (windowManager != null) {
mQueue.poll();
windowManager.removeView(toastView);
sendMessageDelayed(superToast,
Messages.DISPLAY_SUPERTOAST, 500);
if(superToast.getOnDismissListener() != null) {
superToast.getOnDismissListener().onDismiss(superToast.getView());
}
}
}
/* Cancels/removes all showing pending SuperToasts */
protected void cancelAllSuperToasts() {
removeMessages(Messages.ADD_SUPERTOAST);
removeMessages(Messages.DISPLAY_SUPERTOAST);
removeMessages(Messages.REMOVE_SUPERTOAST);
for (SuperToast superToast : mQueue) {
if (superToast.isShowing()) {
superToast.getWindowManager().removeView(
superToast.getView());
}
}
mQueue.clear();
}
}
public class Style {
public static final int BLACK = 0;
public static final int BLUE = 1;
public static final int GRAY = 2;
public static final int GREEN = 3;
public static final int ORANGE = 4;
public static final int PURPLE = 5;
public static final int RED = 6;
public static final int WHITE = 7;
public SuperToast.Animations animations = SuperToast.Animations.FADE;
public int background = getBackground(GRAY);
public int typefaceStyle = Typeface.NORMAL;
public int textColor = Color.WHITE;
public int dividerColor = Color.WHITE;
public int buttonTextColor = Color.LTGRAY;
/**
* Returns a preset style.
*
* @param styleType {@link Style}
*
* @return {@link Style}
*/
public static Style getStyle(int styleType) {
final Style style = new Style();
switch (styleType) {
case BLACK:
style.textColor = Color.WHITE;
style.background = getBackground(BLACK);
style.dividerColor = Color.WHITE;
return style;
case WHITE:
style.textColor = Color.DKGRAY;
style.background = getBackground(WHITE);
style.dividerColor = Color.DKGRAY;
style.buttonTextColor = Color.GRAY;
return style;
case GRAY:
style.textColor = Color.WHITE;
style.background = getBackground(GRAY);
style.dividerColor = Color.WHITE;
style.buttonTextColor = Color.GRAY;
return style;
case PURPLE:
style.textColor = Color.WHITE;
style.background = getBackground(PURPLE);
style.dividerColor = Color.WHITE;
return style;
case RED:
style.textColor = Color.WHITE;
style.background = getBackground(RED);
style.dividerColor = Color.WHITE;
return style;
case ORANGE:
style.textColor = Color.WHITE;
style.background = getBackground(ORANGE);
style.dividerColor = Color.WHITE;
return style;
case BLUE:
style.textColor = Color.WHITE;
style.background = getBackground(BLUE);
style.dividerColor = Color.WHITE;
return style;
case GREEN:
style.textColor = Color.WHITE;
style.background = getBackground(GREEN);
style.dividerColor = Color.WHITE;
return style;
default:
style.textColor = Color.WHITE;
style.background = getBackground(GRAY);
style.dividerColor = Color.WHITE;
return style;
}
}
/**
* Returns a preset style with specified animations.
*
* @param styleType {@link Style}
* @param animations {@link com.github.johnpersano.supertoasts.SuperToast.Animations}
*
* @return {@link Style}
*/
public static Style getStyle(int styleType, SuperToast.Animations animations) {
final Style style = new Style();
style.animations = animations;
switch (styleType) {
case BLACK:
style.textColor = Color.WHITE;
style.background = getBackground(BLACK);
style.dividerColor = Color.WHITE;
return style;
case WHITE:
style.textColor = Color.DKGRAY;
style.background = getBackground(WHITE);
style.dividerColor = Color.DKGRAY;
style.buttonTextColor = Color.GRAY;
return style;
case GRAY:
style.textColor = Color.WHITE;
style.background = getBackground(GRAY);
style.dividerColor = Color.WHITE;
style.buttonTextColor = Color.GRAY;
return style;
case PURPLE:
style.textColor = Color.WHITE;
style.background = getBackground(PURPLE);
style.dividerColor = Color.WHITE;
return style;
case RED:
style.textColor = Color.WHITE;
style.background = getBackground(RED);
style.dividerColor = Color.WHITE;
return style;
case ORANGE:
style.textColor = Color.WHITE;
style.background = getBackground(ORANGE);
style.dividerColor = Color.WHITE;
return style;
case BLUE:
style.textColor = Color.WHITE;
style.background = getBackground(BLUE);
style.dividerColor = Color.WHITE;
return style;
case GREEN:
style.textColor = Color.WHITE;
style.background = getBackground(GREEN);
style.dividerColor = Color.WHITE;
return style;
default:
style.textColor = Color.WHITE;
style.background = getBackground(GRAY);
style.dividerColor = Color.WHITE;
return style;
}
}
public static int getBackground(int style) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
switch (style) {
case BLACK:
return (R.drawable.background_kitkat_black);
case WHITE:
return (R.drawable.background_kitkat_white);
case GRAY:
return (R.drawable.background_kitkat_gray);
case PURPLE:
return (R.drawable.background_kitkat_purple);
case RED:
return (R.drawable.background_kitkat_red);
case ORANGE:
return (R.drawable.background_kitkat_orange);
case BLUE:
return (R.drawable.background_kitkat_blue);
case GREEN:
return (R.drawable.background_kitkat_green);
default:
return (R.drawable.background_kitkat_gray);
}
} else {
switch (style) {
case BLACK:
return (R.drawable.background_standard_black);
case WHITE:
return (R.drawable.background_standard_white);
case GRAY:
return (R.drawable.background_standard_gray);
case PURPLE:
return (R.drawable.background_standard_purple);
case RED:
return (R.drawable.background_standard_red);
case ORANGE:
return (R.drawable.background_standard_orange);
case BLUE:
return (R.drawable.background_standard_blue);
case GREEN:
return (R.drawable.background_standard_green);
default:
return (R.drawable.background_standard_gray);
}
}
}
}
public class SuperToast {
private static final String TAG = "SuperToast";
private static final String ERROR_CONTEXTNULL = " - You cannot use a null context.";
private static final String ERROR_DURATIONTOOLONG = " - You should NEVER specify a duration greater than " +
"four and a half seconds for a SuperToast.";
/**
* Custom OnClickListener to be used with SuperActivityToasts/SuperCardToasts. Note that
* SuperActivityToasts/SuperCardToasts must use this with an
* {@link com.github.johnpersano.supertoasts.util.OnClickWrapper}
*/
public interface OnClickListener {
public void onClick(View view, Parcelable token);
}
/**
* Custom OnDismissListener to be used with any type of SuperToasts. Note that
* SuperActivityToasts/SuperCardToasts must use this with an
* {@link com.github.johnpersano.supertoasts.util.OnDismissWrapper}
*/
public interface OnDismissListener {
public void onDismiss(View view);
}
/**
* Backgrounds for all types of SuperToasts.
*/
public static class Background {
public static final int BLACK = Style.getBackground(Style.BLACK);
public static final int BLUE = Style.getBackground(Style.BLUE);
public static final int GRAY = Style.getBackground(Style.GRAY);
public static final int GREEN = Style.getBackground(Style.GREEN);
public static final int ORANGE = Style.getBackground(Style.ORANGE);
public static final int PURPLE = Style.getBackground(Style.PURPLE);
public static final int RED = Style.getBackground(Style.RED);
public static final int WHITE = Style.getBackground(Style.WHITE);
}
/**
* Animations for all types of SuperToasts.
*/
public enum Animations {
FADE,
FLYIN,
SCALE,
POPUP
}
/**
* Icons for all types of SuperToasts.
*/
public static class Icon {
/**
* Icons for all types of SuperToasts with a dark background.
*/
public static class Dark {
public static final int EDIT = (R.drawable.icon_dark_edit);
public static final int EXIT = (R.drawable.icon_dark_exit);
public static final int INFO = (R.drawable.icon_dark_info);
public static final int REDO = (R.drawable.icon_dark_redo);
public static final int REFRESH = (R.drawable.icon_dark_refresh);
public static final int SAVE = (R.drawable.icon_dark_save);
public static final int SHARE = (R.drawable.icon_dark_share);
public static final int UNDO = (R.drawable.icon_dark_undo);
}
/**
* Icons for all types of SuperToasts with a light background.
*/
public static class Light {
public static final int EDIT = (R.drawable.icon_light_edit);
public static final int EXIT = (R.drawable.icon_light_exit);
public static final int INFO = (R.drawable.icon_light_info);
public static final int REDO = (R.drawable.icon_light_redo);
public static final int REFRESH = (R.drawable.icon_light_refresh);
public static final int SAVE = (R.drawable.icon_light_save);
public static final int SHARE = (R.drawable.icon_light_share);
public static final int UNDO = (R.drawable.icon_light_undo);
}
}
/**
* Durations for all types of SuperToasts.
*/
public static class Duration {
public static final int VERY_SHORT = (1500);
public static final int SHORT = (2000);
public static final int MEDIUM = (2750);
public static final int LONG = (3500);
public static final int EXTRA_LONG = (4500);
}
/**
* Text sizes for all types of SuperToasts.
*/
public static class TextSize {
public static final int EXTRA_SMALL = (12);
public static final int SMALL = (14);
public static final int MEDIUM = (16);
public static final int LARGE = (18);
}
/**
* Types for SuperActivityToasts and SuperCardToasts.
*/
public enum Type {
/**
* Standard type used for displaying messages.
*/
STANDARD,
/**
* Progress type used for showing progress.
*/
PROGRESS,
/**
* Progress type used for showing progress.
*/
PROGRESS_HORIZONTAL,
/**
* Button type used for receiving click actions.
*/
BUTTON
}
/**
* Positions for icons used in all types of SuperToasts.
*/
public enum IconPosition {
/**
* Set the icon to the left of the text.
*/
LEFT,
/**
* Set the icon to the right of the text.
*/
RIGHT,
/**
* Set the icon on top of the text.
*/
TOP,
/**
* Set the icon on the bottom of the text.
*/
BOTTOM
}
private Animations mAnimations = Animations.FADE;
private Context mContext;
private int mGravity = Gravity.BOTTOM | Gravity.CENTER;
private int mDuration = Duration.SHORT;
private int mTypefaceStyle;
private int mBackground;
private int mXOffset = 0;
private int mYOffset = 0;
private LinearLayout mRootLayout;
private OnDismissListener mOnDismissListener;
private TextView mMessageTextView;
private View mToastView;
private WindowManager mWindowManager;
private WindowManager.LayoutParams mWindowManagerParams;
/**
* Instantiates a new {@value #TAG}.
*
* @param context {@link android.content.Context}
*/
public SuperToast(Context context) {
if (context == null) {
throw new IllegalArgumentException(TAG + ERROR_CONTEXTNULL);
}
this.mContext = context;
mYOffset = context.getResources().getDimensionPixelSize(
R.dimen.toast_hover);
final LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mToastView = layoutInflater.inflate(R.layout.supertoast, null);
mWindowManager = (WindowManager) mToastView.getContext()
.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
mRootLayout = (LinearLayout)
mToastView.findViewById(R.id.root_layout);
mMessageTextView = (TextView)
mToastView.findViewById(R.id.message_textview);
}
/**
* Instantiates a new {@value #TAG} with a specified style.
*
* @param context {@link android.content.Context}
* @param style {@link com.github.johnpersano.supertoasts.util.Style}
*/
public SuperToast(Context context, Style style) {
if (context == null) {
throw new IllegalArgumentException(TAG + ERROR_CONTEXTNULL);
}
this.mContext = context;
mYOffset = context.getResources().getDimensionPixelSize(
R.dimen.toast_hover);
final LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mToastView = layoutInflater.inflate(R.layout.supertoast, null);
mWindowManager = (WindowManager) mToastView.getContext()
.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
mRootLayout = (LinearLayout)
mToastView.findViewById(R.id.root_layout);
mMessageTextView = (TextView)
mToastView.findViewById(R.id.message_textview);
this.setStyle(style);
}
/**
* Shows the {@value #TAG}. If another {@value #TAG} is showing than
* this one will be added to a queue and shown when the previous {@value #TAG}
* is dismissed.
*/
public void show() {
mWindowManagerParams = new WindowManager.LayoutParams();
mWindowManagerParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
mWindowManagerParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
mWindowManagerParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;
mWindowManagerParams.format = PixelFormat.TRANSLUCENT;
mWindowManagerParams.windowAnimations = getAnimation();
mWindowManagerParams.type = WindowManager.LayoutParams.TYPE_TOAST;
mWindowManagerParams.gravity = mGravity;
mWindowManagerParams.x = mXOffset;
mWindowManagerParams.y = mYOffset;
ManagerSuperToast.getInstance().add(this);
}
/**
* Sets the message text of the {@value #TAG}.
*
* @param text {@link CharSequence}
*/
public void setText(CharSequence text) {
mMessageTextView.setText(text);
}
/**
* Returns the message text of the {@value #TAG}.
*
* @return {@link CharSequence}
*/
public CharSequence getText() {
return mMessageTextView.getText();
}
/**
* Sets the message typeface style of the {@value #TAG}.
*
* @param typeface {@link android.graphics.Typeface} int
*/
public void setTypefaceStyle(int typeface) {
mTypefaceStyle = typeface;
mMessageTextView.setTypeface(mMessageTextView.getTypeface(), typeface);
}
/**
* Returns the message typeface style of the {@value #TAG}.
*
* @return {@link android.graphics.Typeface} int
*/
public int getTypefaceStyle() {
return mTypefaceStyle;
}
/**
* Sets the message text color of the {@value #TAG}.
*
* @param textColor {@link android.graphics.Color}
*/
public void setTextColor(int textColor) {
mMessageTextView.setTextColor(textColor);
}
/**
* Returns the message text color of the {@value #TAG}.
*
* @return int
*/
public int getTextColor() {
return mMessageTextView.getCurrentTextColor();
}
/**
* Sets the text size of the {@value #TAG} message.
*
* @param textSize int
*/
public void setTextSize(int textSize) {
mMessageTextView.setTextSize(textSize);
}
/**
* Returns the text size of the {@value #TAG} message in pixels.
*
* @return float
*/
public float getTextSize() {
return mMessageTextView.getTextSize();
}
/**
* Sets the duration that the {@value #TAG} will show.
*
* @param duration {@link com.github.johnpersano.supertoasts.SuperToast.Duration}
*/
public void setDuration(int duration) {
if(duration > Duration.EXTRA_LONG) {
Log.e(TAG, TAG + ERROR_DURATIONTOOLONG);
this.mDuration = Duration.EXTRA_LONG;
} else {
this.mDuration = duration;
}
}
/**
* Returns the duration of the {@value #TAG}.
*
* @return int
*/
public int getDuration() {
return this.mDuration;
}
/**
* Sets an icon resource to the {@value #TAG} with a specified position.
*
* @param iconResource {@link com.github.johnpersano.supertoasts.SuperToast.Icon}
* @param iconPosition {@link com.github.johnpersano.supertoasts.SuperToast.IconPosition}
*/
public void setIcon(int iconResource, IconPosition iconPosition) {
if (iconPosition == IconPosition.BOTTOM) {
mMessageTextView.setCompoundDrawablesWithIntrinsicBounds(null, null,
null, mContext.getResources().getDrawable(iconResource));
} else if (iconPosition == IconPosition.LEFT) {
mMessageTextView.setCompoundDrawablesWithIntrinsicBounds(mContext.getResources()
.getDrawable(iconResource), null, null, null);
} else if (iconPosition == IconPosition.RIGHT) {
mMessageTextView.setCompoundDrawablesWithIntrinsicBounds(null, null,
mContext.getResources().getDrawable(iconResource), null);
} else if (iconPosition == IconPosition.TOP) {
mMessageTextView.setCompoundDrawablesWithIntrinsicBounds(null,
mContext.getResources().getDrawable(iconResource), null, null);
}
}
/**
* Sets the background resource of the {@value #TAG}.
*
* @param background {@link com.github.johnpersano.supertoasts.SuperToast.Background}
*/
public void setBackground(int background) {
this.mBackground = background;
mRootLayout.setBackgroundResource(background);
}
/**
* Returns the background resource of the {@value #TAG}.
*
* @return int
*/
public int getBackground() {
return this.mBackground;
}
/**
* Sets the gravity of the {@value #TAG} along with x and y offsets.
*
* @param gravity {@link android.view.Gravity} int
* @param xOffset int
* @param yOffset int
*/
public void setGravity(int gravity, int xOffset, int yOffset) {
this.mGravity = gravity;
this.mXOffset = xOffset;
this.mYOffset = yOffset;
}
/**
* Sets the show/hide animations of the {@value #TAG}.
*
* @param animations {@link com.github.johnpersano.supertoasts.SuperToast.Animations}
*/
public void setAnimations(Animations animations) {
this.mAnimations = animations;
}
/**
* Returns the show/hide animations of the {@value #TAG}.
*
* @return {@link com.github.johnpersano.supertoasts.SuperToast.Animations}
*/
public Animations getAnimations() {
return this.mAnimations;
}
/**
* Sets an OnDismissListener defined in this library
* to the {@value #TAG}. Does not require wrapper.
*
* @param onDismissListener {@link com.github.johnpersano.supertoasts.SuperToast.OnDismissListener}
*/
public void setOnDismissListener(OnDismissListener onDismissListener) {
this.mOnDismissListener = onDismissListener;
}
/**
* Returns the OnDismissListener set to the {@value #TAG}.
*
* @return {@link com.github.johnpersano.supertoasts.SuperToast.OnDismissListener}
*/
public OnDismissListener getOnDismissListener() {
return mOnDismissListener;
}
/**
* Dismisses the {@value #TAG}.
*/
public void dismiss() {
ManagerSuperToast.getInstance().removeSuperToast(this);
}
/**
* Returns the {@value #TAG} message textview.
*
* @return {@link android.widget.TextView}
*/
public TextView getTextView() {
return mMessageTextView;
}
/**
* Returns the {@value #TAG} view.
*
* @return {@link android.view.View}
*/
public View getView() {
return mToastView;
}
/**
* Returns true if the {@value #TAG} is showing.
*
* @return boolean
*/
public boolean isShowing() {
return mToastView != null && mToastView.isShown();
}
/**
* Returns the window manager that the {@value #TAG} is attached to.
*
* @return {@link android.view.WindowManager}
*/
public WindowManager getWindowManager() {
return mWindowManager;
}
/**
* Returns the window manager layout params of the {@value #TAG}.
*
* @return {@link android.view.WindowManager.LayoutParams}
*/
public WindowManager.LayoutParams getWindowManagerParams() {
return mWindowManagerParams;
}
/**
* Private method used to return a specific animation for a animations enum
*/
private int getAnimation() {
if (mAnimations == Animations.FLYIN) {
return android.R.style.Animation_Translucent;
} else if (mAnimations == Animations.SCALE) {
return android.R.style.Animation_Dialog;
} else if (mAnimations == Animations.POPUP) {
return android.R.style.Animation_InputMethod;
} else {
return android.R.style.Animation_Toast;
}
}
/**
* Private method used to set a default style to the {@value #TAG}
*/
private void setStyle(Style style) {
this.setAnimations(style.animations);
this.setTypefaceStyle(style.typefaceStyle);
this.setTextColor(style.textColor);
this.setBackground(style.background);
}
/**
* Returns a standard {@value #TAG}.
*
* @param context {@link android.content.Context}
* @param textCharSequence {@link CharSequence}
* @param durationInteger {@link com.github.johnpersano.supertoasts.SuperToast.Duration}
*
* @return {@link SuperToast}
*/
public static SuperToast create(Context context, CharSequence textCharSequence,
int durationInteger) {
SuperToast superToast = new SuperToast(context);
superToast.setText(textCharSequence);
superToast.setDuration(durationInteger);
return superToast;
}
/**
* Returns a standard {@value #TAG} with specified animations.
*
* @param context {@link android.content.Context}
* @param textCharSequence {@link CharSequence}
* @param durationInteger {@link com.github.johnpersano.supertoasts.SuperToast.Duration}
* @param animations {@link com.github.johnpersano.supertoasts.SuperToast.Animations}
*
* @return {@link SuperToast}
*/
public static SuperToast create(Context context, CharSequence textCharSequence,
int durationInteger, Animations animations) {
final SuperToast superToast = new SuperToast(context);
superToast.setText(textCharSequence);
superToast.setDuration(durationInteger);
superToast.setAnimations(animations);
return superToast;
}
/**
* Returns a {@value #TAG} with a specified style.
*
* @param context {@link android.content.Context}
* @param textCharSequence {@link CharSequence}
* @param durationInteger {@link com.github.johnpersano.supertoasts.SuperToast.Duration}
* @param style {@link com.github.johnpersano.supertoasts.util.Style}
*
* @return SuperCardToast
*/
public static SuperToast create(Context context, CharSequence textCharSequence, int durationInteger, Style style) {
final SuperToast superToast = new SuperToast(context);
superToast.setText(textCharSequence);
superToast.setDuration(durationInteger);
superToast.setStyle(style);
return superToast;
}
/**
* Dismisses and removes all showing/pending {@value #TAG}.
*/
public static void cancelAllSuperToasts() {
ManagerSuperToast.getInstance().cancelAllSuperToasts();
}
}
Code见 https://github.com/huanyi0723/TestToast
Android 自定义Toast的更多相关文章
- 朝花夕拾-android 自定义toast
在一个只有你而且还未知的世界中,不去探索未知,死守一处,你到底在守什么呢? 作为一个目前的android程序员,可能过去写着delphi的代码,可能未来回去搭建服务器.不管怎样,你现在是一名安卓程序员 ...
- android 自定义Toast显示风格
1.创建一个自己想要显示Toast风格的XML如下代码(toast_xml.xml): <?xml version="1.0" encoding="utf-8&qu ...
- 023 Android 自定义Toast控件
1.Toast自定义控件工具类 package com.example.administrator.test62360safeguard.Utils; import android.content.C ...
- Android 自定义Toast,不使用系统Toast
效果图: 创建Toast类 package com.example.messageboxtest; import android.app.Activity; import android.conten ...
- Android自定义Toast宽度无法设置问题解决
在项目中想要实现一个头部的toast提示效果,类似下图 再实现的过程中发现,如果直接通过修改Toast的View布局的父控件宽度是无法实现效果的,后来是通过直接用代码指定父控件内部的textview ...
- Android自定义Toast
1.http://www.cnblogs.com/salam/archive/2010/11/10/1873654.html 2.
- Android带图片的Toast(自定义Toast)
使用Android默认的Toast Toast简介: Toast是一个简单的消息显示框,能够短暂的出现在屏幕的某个位置,显示提示消息. 默认的位置是屏幕的下方正中,一般Toast的使用如下: Toas ...
- Android Toast 设置到屏幕中间,自定义Toast的实现方法,及其说明
http://blog.csdn.net/wangfayinn/article/details/8065763 Android Toast用于在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失. ...
- Android开发必知--自定义Toast提示
开发过Android的童鞋都会遇到一个问题,就是在打印Toast提示时,如果短时间内触发多个提示,就会造成Toast不停的重复出现,直到被触发的Toast全部显示完为止.这虽然不是什么大毛病,但在用户 ...
随机推荐
- 【转】解决svn Authorization failed错误
转载地址:http://blog.sina.com.cn/s/blog_4b93170a0100leb2.html 出现该问题基本都是三个配置文件的问题,下面把这个文件列出来 svnserve.con ...
- 用ThreadLocal为线程生成唯一标识及实现原理
1.在多线程编程中,有时候需要自动为每个启动的线程生成一个唯一标识,这个时候,通过一个ThreadLocal变量来保存每个线程的标识是最有效.最方便的方式了. 2.ThreadLocal 实例通常是类 ...
- JQuery知识快览之二—事件
事件是脚本语言的核心.本文将为大家介绍JQuery支持的一些事件和如何自定义事件 JQuery内置事件 1.Document加载事件 JQuery提供了ready,load,unload三个Docum ...
- 20150916_001 vba 基础
一.什么是“宏”.“宏”有什么用 关于“宏”的详细定义,可以参考百度百科的解释(点击查看).我给它一个简单的或许不太严谨的定义: 宏的通俗定义:宏是被某些软件所能识别.理解并执行的特定代码/脚本. 宏 ...
- Adam 演示demo内容整理
在这个6个多G的演示demo中,还是发现了不少东西. 这篇文章八卦向的东西比较多,不过支持abc格式的话,做Cutscene一下子多了很多可以用的东西. 1.在插件目录下发现了ABC格式的导入dll. ...
- 浅谈MySQL索引背后的数据结构及算法(转载)
转自:http://blogread.cn/it/article/4088?f=wb1 摘要 本文以MySQL数据库为研究对象,讨论与数据库索引相关的一些话题.特别需要说明的是,MySQL支持诸多存储 ...
- SQL与C#结合完整修改 删除信息
--SQl中--建立ren的数据库,插入一条信息 create database ren go use ren go create table xinxi ( code ) primary key,- ...
- java-cmd-命令行编译和运行java文件
一.使用的工具 1.javac 2.java 二.命令 项目目录只这样的 D:/project/src/com/example/Child.java D:/project/src/com/exampl ...
- IIS7多域名绑定同一物理目录,设置不同默认文档的解决方案
转载自 http://zzstudy.offcn.com/archives/6159 如何解决IIS7多域名绑定同一物理目录,设置不同的默认文档的问题? 因为在一个物理目录下只有一个web.confi ...
- ruby学习总结04
1.类和实例的关系 使用[实例.class]查看某个对象属于哪个类 使用[实例.instance_of(类名)]判断该实例是否属于某个类 使用[实例.instance_methods]查看类的所有实例 ...