Today, I saw an application pop-up Toast, and it seems pretty good to have a sound. I said you don't have to fight, so come and say Toast.
This is not the case. The Code is as follows:
[Java] view plaincopyprint? <SPAN style = "FONT-SIZE: 18px"> <STRONG> package weibo. lixiaodaoaaa. view;
Import weibo. lixiaodaoaaa. ui. R;
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;
/**
* Custom Toast control with sound prompt
*
* @ Author http://weibo.com/lixiaodaoaaa http://t.qq.com/lixiaodaoaaa
* @ Version 0.1
* @ Created 2013-4-23
*/
Public class MyToast extends Toast
{
Private MediaPlayer mPlayer;
Private boolean isSound;
Public MyToast (Context context)
{
This (context, false );
}
// IsSound indicates whether to play music ;;;;
Public MyToast (Context context, boolean isSound)
{
Super (context );
This. isSound = isSound;
MPlayer = MediaPlayer. create (context, R. raw. allsuccess );
MPlayer. setOnCompletionListener (new MediaPlayer. OnCompletionListener ()
{
@ Override
Public void onCompletion (MediaPlayer mp)
{
Mp. release (); // release resources. Release resources ;;
}
});
}
@ Override
Public void show ()
{
Super. show ();
If (isSound)
{
MPlayer. start ();
}
}
/**
* Set whether to play the sound.
*/
Public void setIsSound (boolean isSound)
{
This. isSound = isSound;
}
/**
* Get the control instance
*
* @ Param context
* @ Param text
* Prompt message
* @ Param isSound
* Whether to play the sound
* @ Return
*/
Public static MyToast show (Context context, CharSequence text, boolean isSound, int duration)
{
MyToast result = new MyToast (context, isSound );
LayoutInflater inflate = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE );
DisplayMetrics dm = context. getResources (). getDisplayMetrics ();
View v = inflate. inflate (R. layout. new_data_toast, null );
// V. setMinimumWidth (dm. widthPixels); // set the minimum width of the control to the screen width of the mobile phone.
TextView TV = (TextView) v. findViewById (R. id. new_data_toast_message );
TV. setText (text );
Result. setView (v );
Result. setDuration (duration); // you can specify the display duration ;;;;
Result. setGravity (Gravity. BOTTOM, 0, (int) (dm. density * 85 ));
Return result;
}
}
</STRONG> </SPAN>
Package weibo. lixiaodaoaaa. view;
Import weibo. lixiaodaoaaa. ui. R;
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;
/**
* Custom Toast control with sound prompt
*
* @ Author http://weibo.com/lixiaodaoaaa http://t.qq.com/lixiaodaoaaa
* @ Version 0.1
* @ Created 2013-4-23
*/
Public class MyToast extends Toast
{
Private MediaPlayer mPlayer;
Private boolean isSound;
Public MyToast (Context context)
{
This (context, false );
}
// IsSound indicates whether to play music ;;;;
Public MyToast (Context context, boolean isSound)
{
Super (context );
This. isSound = isSound;
MPlayer = MediaPlayer. create (context, R. raw. allsuccess );
MPlayer. setOnCompletionListener (new MediaPlayer. OnCompletionListener ()
{
@ Override
Public void onCompletion (MediaPlayer mp)
{
Mp. release (); // release resources. Release resources ;;
}
});
}
@ Override
Public void show ()
{
Super. show ();
If (isSound)
{
MPlayer. start ();
}
}
/**
* Set whether to play the sound.
*/
Public void setIsSound (boolean isSound)
{
This. isSound = isSound;
}
/**
* Get the control instance
*
* @ Param context
* @ Param text
* Prompt message
* @ Param isSound
* Whether to play the sound
* @ Return
*/
Public static MyToast show (Context context, CharSequence text, boolean isSound, int duration)
{
MyToast result = new MyToast (context, isSound );
LayoutInflater inflate = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE );
DisplayMetrics dm = context. getResources (). getDisplayMetrics ();
View v = inflate. inflate (R. layout. new_data_toast, null );
// V. setMinimumWidth (dm. widthPixels); // set the minimum width of the control to the screen width of the mobile phone.
TextView TV = (TextView) v. findViewById (R. id. new_data_toast_message );
TV. setText (text );
Result. setView (v );
Result. setDuration (duration); // you can specify the display duration ;;;;
Result. setGravity (Gravity. BOTTOM, 0, (int) (dm. density * 85 ));
Return result;
}
}
The display effect is as follows: