Implement Toast with sound prompt in Android (custom extension Toast)

Source: Internet
Author: User

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:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.