Back to project page Common-Library.
The source code is released under:
Apache License
If you think the Android project Common-Library listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.morgan.library.widget; //from w w w . j a va 2 s.co m import android.content.Context; import android.media.MediaPlayer; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.widget.TextView; import android.widget.Toast; import com.morgan.library.R; /** * ??????Toast,???????????????? * * @author Morgan.Ji * */ public class CustomToast extends Toast { private MediaPlayer mPlayer; private boolean mNeedSound; public CustomToast(Context context) { this(context, false); } public CustomToast(Context context, boolean needSound) { super(context); this.mNeedSound = needSound; mPlayer = MediaPlayer.create(context, R.raw.customtoast); mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { mp.release(); } }); } @Override public void show() { super.show(); if (mNeedSound) { mPlayer.start(); } } /** * ?????????? */ public void setNeedSound(boolean needSound) { this.mNeedSound = needSound; } /** * ???????? * * @param context * @param text * ???????? * @param needSound * ???????? * @return */ public static CustomToast makeText(Context context, CharSequence text, boolean needSound) { CustomToast result = new CustomToast(context, needSound); LayoutInflater inflate = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); DisplayMetrics dm = context.getResources().getDisplayMetrics(); View v = inflate.inflate(R.layout.custom_toast, null); v.setMinimumWidth(dm.widthPixels);// ????????????????? TextView tv = (TextView) v.findViewById(R.id.toast_message); tv.setText(text); result.setView(v); result.setDuration(600); result.setGravity(Gravity.TOP, 0, (int) (dm.density * 75)); return result; } }