封装 Toast
一、 ToastView.java
1 import android.content.Context; 2 import android.view.LayoutInflater; 3 import android.view.View; 4 import android.widget.ImageView; 5 import android.widget.LinearLayout; 6 import android.widget.TextView; 7 import android.widget.Toast; 8 9 10 public class ToastView extends LinearLayout{ 11 private static ToastView mToastView; 12 13 private TextView toastText; 14 private ImageView toastIcon; 15 private Context mContext; 16 17 private View layout; 18 private Toast mToast; 19 20 21 private ToastView(Context context) { 22 super(context,null); 23 mContext = context; 24 LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 25 layout = inflater.inflate(R.layout.toast, null); 26 27 toastIcon = (ImageView) layout.findViewById(R.id.toastIcon); 28 toastText = (TextView) layout.findViewById(R.id.toastText); 29 } 30 31 public static ToastView getInstance(Context context) { 32 if (mToastView != null) { 33 return mToastView; 34 } else { 35 if (context != null) { 36 mToastView = new ToastView(context); 37 return mToastView; 38 } else { 39 return null; 40 } 41 } 42 } 43 44 public void setIconVisiblity(int visiblity){ 45 toastIcon.setVisibility(visiblity); 46 } 47 48 public void showToast(String str,int gravity,int xoffset,int yoffest) { 49 toastText.setText(str); 50 51 if(mToast!=null){ 52 mToast.setGravity(gravity,xoffset,yoffest); 53 mToast.show(); 54 }else{ 55 mToast= new Toast(mContext); 56 mToast.setView(layout); 57 mToast.setGravity(gravity,xoffset,yoffest); 58 mToast.setDuration(Toast.LENGTH_SHORT); 59 mToast.show(); 60 } 61 } 62 63 public void showToast(String str,int gravity,int xoffset,int yoffest,int duration) { 64 toastText.setText(str); 65 duration = duration<=0 ? Toast.LENGTH_SHORT : duration; 66 67 if(mToast!=null){ 68 mToast.setGravity(gravity,xoffset,yoffest); 69 mToast.setDuration(duration); 70 mToast.show(); 71 }else{ 72 mToast= new Toast(mContext); 73 mToast.setView(layout); 74 mToast.setGravity(gravity,xoffset,yoffest); 75 mToast.setDuration(duration); 76 mToast.show(); 77 } 78 } 79 80 }
二、toast.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="wrap_content" 5 android:gravity="center_vertical"> 6 7 8 <TextView 9 android:id="@+id/toastText" 10 android:layout_width="wrap_content" 11 android:layout_height="@dimen/toastHeight" 12 android:textColor="@color/title_text_color" 13 android:background="@drawable/toast_bg" 14 android:gravity="center" 15 android:paddingLeft="@dimen/toastTextPaddingLeft" 16 android:paddingRight="@dimen/toastTextPaddingRight" 17 android:textSize="@dimen/toastTextSize"/> 18 19 <ImageView 20 android:id="@+id/toastIcon" 21 android:layout_width="@dimen/toastIconWidth" 22 android:layout_height="@dimen/toastIconWidth" 23 android:layout_marginLeft="@dimen/toastIconWidth" 24 android:scaleType="fitXY" 25 android:layout_centerVertical="true" 26 android:src="@drawable/toast_icon" /> 27 </RelativeLayout>
三、Acitivity中调用
//自定义的 private void showToast(String str, int offset) { ToastView toast = ToastView.getInstance(RecommendActivity.this); if(offset < 0){ toast.showToast(str, Gravity.TOP | Gravity.CENTER_HORIZONTAL,0, //默认时长的Toast (int)getResources().getDimension(R.dimen.defaultToastMarginTop)); }else{ toast.showToast(str, Gravity.TOP | Gravity.CENTER_HORIZONTAL,0, //自定义时长的Toast (int)getResources().getDimension(R.dimen.defaultToastMarginTop), offset); } }
封装的这个Toast没什么技术含量。
但是,之所以把Toast定义为单例模式,是为了防止疯狂点击按钮,出现连续不断的Toast
因为系统的Toast是维护一个队列,每次cancle只对当前的Toast 有用。所以之后的Toast还是会不停出现
这个Toast改为单例模式之后。如果当前Toast不为空,只对其进行setText。然后show(不调用show不会出现)
如果当前为空,才会新建一个ToastView。