今天看到一个应用弹出的Toast的同时还 蹦擦个声音 貌似还不错。我说你别得瑟了,哥也搞个Toast也出来冒个声 也来得瑟下。
这不,代码奉上:
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自定义 Toast控件
*
* @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 表示是否播放音乐;;;;
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();// 释放资源。让资源得到释放;;
}
});
}
@Override
public void show()
{
super.show();
if (isSound)
{
mPlayer.start();
}
}
/**
* 设置是否播放声音
*/
public void setIsSound(boolean isSound)
{
this.isSound = isSound;
}
/**
* 获取控件实例
*
* @param context
* @param text
* 提示消息
* @param isSound
* 是否播放声音
* @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);// 设置控件最小宽度为手机屏幕宽度
TextView tv = (TextView) v.findViewById(R.id.new_data_toast_message);
tv.setText(text);
result.setView(v);
result.setDuration(duration);// 设置 显示多长时间;;;;
result.setGravity(Gravity.BOTTOM, 0, (int) (dm.density * 85));
return result;
}
}
显示效果如下:
测试工程 Demo实例下载(演示Demo下载)
1. 猛击这里下载。