Android-toast-详解-实例-优化

实例:http://www.jb51.net/article/101948.htm

public class MainActivity extends Activity implements OnClickListener {
  Handler handler = new Handler();
 
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 
    findViewById(R.id.btnSimpleToast).setOnClickListener(this);
    findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
        this);
    findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
    findViewById(R.id.btnCustomToast).setOnClickListener(this);
    findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);
 
  }
 
  public void showToast() {
    handler.post(new Runnable() {
 
      @Override
      public void run() {
        Toast.makeText(getApplicationContext(), "我来自其他线程!",
            Toast.LENGTH_SHORT).show();
 
      }
    });
  }
 
  @Override
  public void onClick(View v) {
    Toast toast = null;
    switch (v.getId()) {
    case R.id.btnSimpleToast:
      Toast.makeText(getApplicationContext(), "默认Toast样式",
          Toast.LENGTH_SHORT).show();
      break;
    case R.id.btnSimpleToastWithCustomPosition:
      toast = Toast.makeText(getApplicationContext(), "自定义位置Toast",
          Toast.LENGTH_LONG);
      toast.setGravity(Gravity.CENTER, 0, 0);
      toast.show();
      break;
    case R.id.btnSimpleToastWithImage:
      toast = Toast.makeText(getApplicationContext(), "带图片的Toast",
          Toast.LENGTH_LONG);
      toast.setGravity(Gravity.CENTER, 0, 0);
      LinearLayout toastView = (LinearLayout) toast.getView();
      ImageView imageCodeProject = new ImageView(getApplicationContext());
      imageCodeProject.setImageResource(R.drawable.ic_launcher);
      toastView.addView(imageCodeProject, 0);
      toast.show();
      break;
    case R.id.btnCustomToast:
      LayoutInflater inflater = getLayoutInflater();
      View layout = inflater.inflate(R.layout.custom,
          (ViewGroup) findViewById(R.id.llToast));
      ImageView image = (ImageView) layout
          .findViewById(R.id.tvImageToast);
      image.setImageResource(R.drawable.ic_launcher);
      TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
      title.setText("Attention");
      TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
      text.setText("完全自定义Toast");
      toast = new Toast(getApplicationContext());
      toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
      toast.setDuration(Toast.LENGTH_LONG);
      toast.setView(layout);
      toast.show();
      break;
    case R.id.btnRunToastFromOtherThread:
      new Thread(new Runnable() {
        public void run() {
          showToast();
        }
      }).start();
      break;
 
    }
 
  }
 
}
优化:http://blog.csdn.net/u012575819/article/details/51194160


使用Toast一般会用下面这条语句:

Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_SHORT).show();
 
 
  • 1
  • 1

但有一个问题,如果频繁使用toast会导致其长时间显示。

这和Toast的实现原理是有关系的。 
Toast在调用其show方法时,并不是立刻显示出来,而是添加到系统的一个队列中。 
而在这个队列中,先添加进去的Toast先被显示出来,所以说,刚show的Toast并不会立刻显示,而是要等队列中之前被添加的Toast显示完之后才可以显示。 
而这个队列中是可以保存相当多数量的Toast的,如果你一次性new出多个Toast并将它们都show了一遍,系统就会依次一个个将这些Toast全部显示一遍,时间很长,可能你应用都退了,但是Toast还在显示。

(PS:这种情况并不少见。可能你设置用户点击一次按钮显示一次Toast,谁也不能保证用户不会一直狂按这个按钮。) 
这样的情况其实是很影响用户体验的。 
但有解决办法。

先贴代码:

private static Toast myToast;
public static void showToast(Context context, String str) {
        if(myToast == null) {
            myToast = Toast.makeText(context, str, Toast.LENGTH_SHORT);
        } else {
            myToast.setText(str);
        }
        myToast.show();
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

在new一个Toast之前先判断这个myToast是不是为NULL,如果不是NULL,就不要再去new了,以免队列中存放过多的Toast。 
如果不为NULL,只是改变了myToast的显示文字,并调用show()方法。 
这样就避免队列中产生大量Toast了。

另外,贴两个自定义Toast的代码:

这是一个添加了一张图片的Toast。 
其实看Toast的源码就知道,Android默认的Toast的布局就是一个LinearLayout里面一个TextView。 
使用:

(LinearLayout)Toast.getView();
 
 
  • 1
  • 1

拿到这个View并强转成LinearLayout就可以随意向其中增加或移除View了。

private static Toast myIconToast;
public static void showIconToast(Context context, String str, int resId) {

        if(myIconToast == null) {
            myIconToast = Toast.makeText(context, str, Toast.LENGTH_SHORT);
            myIconToast.setGravity(Gravity.CENTER, 0, 0);
            LinearLayout toastLinearLayout = (LinearLayout) myIconToast.getView();
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(resId);
            toastLinearLayout.addView(imageView, 0);
        } else {
            myIconToast.setText(str);
            LinearLayout toastLinearLayout = (LinearLayout) myToast.getView();
            ImageView imageView = new ImageView(context);
            imageView.setImageResource(resId);
            toastLinearLayout.removeViewAt(0);
            toastLinearLayout.addView(imageView, 0);
        }
        myIconToast.show();
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

第二个是一个完全自定义的Toast,布局由我们自己定义, 
利用LayoutInflater拿到我们自定义的布局后,使用:

Toast.setView()
 
 
  • 1
  • 1

即可实现自定义布局的Toast。

private static Toast myCustomToast;
public static void showCustomToast(Activity activity, String title, String text, int resId) {

        LayoutInflater inflater = activity.getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom_toast_layout, null);

        TextView titleView = (TextView) layout.findViewById(R.id.tvTitleToast);
        titleView.setText(title);
        ImageView imageView = (ImageView) layout.findViewById(R.id.tvImageToast);
        imageView.setImageResource(resId);
        TextView textView = (TextView) layout.findViewById(R.id.tvTextToast);
        textView.setText(text);

        if(myCustomToast == null) {
            myCustomToast = new Toast(activity);
            myCustomToast.setGravity(Gravity.RIGHT | Gravity.TOP, 300, 300);
            myCustomToast.setDuration(Toast.LENGTH_SHORT);
            myCustomToast.setView(layout);
        } else {
            myCustomToast.setView(layout);
        }
        myCustomToast.show();
    }
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

需要注意的是,这几个方法里使用的Toast都是成员变量。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值