实例: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一般会用下面这条语句:
- 1
- 1
但有一个问题,如果频繁使用toast会导致其长时间显示。
这和Toast的实现原理是有关系的。
Toast在调用其show方法时,并不是立刻显示出来,而是添加到系统的一个队列中。
而在这个队列中,先添加进去的Toast先被显示出来,所以说,刚show的Toast并不会立刻显示,而是要等队列中之前被添加的Toast显示完之后才可以显示。
而这个队列中是可以保存相当多数量的Toast的,如果你一次性new出多个Toast并将它们都show了一遍,系统就会依次一个个将这些Toast全部显示一遍,时间很长,可能你应用都退了,但是Toast还在显示。
(PS:这种情况并不少见。可能你设置用户点击一次按钮显示一次Toast,谁也不能保证用户不会一直狂按这个按钮。)
这样的情况其实是很影响用户体验的。
但有解决办法。
先贴代码:
- 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。
使用:
- 1
- 1
拿到这个View并强转成LinearLayout就可以随意向其中增加或移除View了。
- 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拿到我们自定义的布局后,使用:
- 1
- 1
即可实现自定义布局的Toast。
- 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都是成员变量。