今天看到一個應用彈出的Toast的同時還 蹦擦個聲音 貌似還不錯。我說你別得瑟了,哥也搞個Toast也出來冒個聲 也來得瑟下。
這不,代碼奉上:
[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;
/**
* 帶聲音提示的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;
}
}
</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;
/**
* 帶聲音提示的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;
}
}
顯示效果如下: