• Android中实现带声音提示的Toast (自定义扩展Toast)


    今天看到一个应用弹出的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下载)  猛击这里下载。

  • 相关阅读:
    NodeJS学习笔记(三) 模块与包_深入学习
    NodeJS学习笔记(二) 模块与包_基础部分
    Java 开发环境搭建
    Quartz.net 定时任务矿建Demo
    MVC AJAX.BeginForm() 页面异步提交
    源码学习分享
    WPF系能优化
    谈如何阅读框架源码
    Linux源码学习(7) 2013-3-1
    Linux源码学习(6) 2013-3-1
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3037317.html
Copyright © 2020-2023  润新知