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.

No,CodeOffer:

 

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;/*** toast custom toast controls with audible prompts ** @ author http://weibo.com/lixiaodaoaaa http://t.qq.com/lixia Odaoaaa * @ version 0.1 * @ created 2013-4-23 */public class mytoast extends toast {private mediaplayer mplayer; private Boolean issound; Public mytoast (context) {This (context, false );} // issound indicates whether to play music; Public mytoast (context, Boolean issound) {super (context); this. issound = issound; mplayer = mediaplayer. create (context, R. raw. allsuccess); mplayer. setoncompletionlistener (ne W 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 * prompts the message * @ Param issound * Whether to play the sound * @ return */public static mytoast show (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 widget width to the mobile phone screen width textview TV = (textview) v. findviewbyid (R. id. new_data_toast_message); TV. settext (text); result. setview (V); result. setduration (duration); // set the display duration; result. setgravity (gravity. bottom, 0, (INT) (DM. density * 85); return result ;}}



 

The display effect is as follows:

Download the demo of the test project (Demo download) and click here to download it.

 

Related Article

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.