Toast 使用小技巧
2012-12-03 11:48
337 查看
Toast 使用过程时,需要自定义显示布局,规避多次点击问题,自己常用的两种方法。
1.自定义一个Toast 工具类,实现自定义布局等。
public class ToastUtils {
private static Handler handler = new Handler(Looper.getMainLooper());
private static Toast toast = null;
private static Object synObj = new Object();
public static void showToast(final Context context, final String msg,boolean flag) {
showToast(context, msg, Toast.LENGTH_SHORT,flag);
}
public static void showToast(final Context context, final int resId,boolean flag) {
showToast(context, resId, Toast.LENGTH_SHORT,flag);
}
public static void showToast(final Context context, final String msg,
final int len,final boolean flag) {
new Thread(new Runnable() {
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
synchronized (synObj) {// 同步锁
if (toast != null) {
toast.cancel();
toast.setDuration(len);
} else {
toast = Toast.makeText(context, msg, len);
View layout = LayoutInflater.from(context)
.inflate(R.layout.my_toast, null);// 自定义布局
toast.setView(layout);
}
//根据不同的情况显示不同的Toast信息
if(flag){
((TextView)toast.getView().findViewById(R.id.TextViewShortInfo)).setVisibility(4);
((TextView)toast.getView().findViewById(R.id.TextViewLongInfo)).setVisibility(0);
((TextView) toast.getView().findViewById(R.id.TextViewLongInfo)).setText(msg);
}else{
((TextView)toast.getView().findViewById(R.id.TextViewLongInfo)).setVisibility(4);
((TextView)toast.getView().findViewById(R.id.TextViewShortInfo)).setVisibility(0);
((TextView) toast.getView().findViewById(R.id.TextViewShortInfo)).setText(msg);
}
toast.show();
}
}
});
}
}).start();
}
public static void showToast(final Context context, final int resId,
final int len,final boolean flag) {
new Thread(new Runnable() {
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
synchronized (synObj) {// 同步锁
if(toast != null) {
toast.cancel();
toast.setDuration(len);
} else {
toast = Toast.makeText(context, resId, len);
View layout = LayoutInflater.from(context)
.inflate(R.layout.my_toast, null);// 自定义布局
toast.setView(layout);
}
if(flag){
((TextView)toast.getView().findViewById(R.id.TextViewShortInfo)).setVisibility(8);
((TextView)toast.getView().findViewById(R.id.TextViewLongInfo)).setVisibility(0);
((TextView) toast.getView().findViewById(R.id.TextViewLongInfo)).setText(resId);
}else{
((TextView)toast.getView().findViewById(R.id.TextViewLongInfo)).setVisibility(8);
((TextView)toast.getView().findViewById(R.id.TextViewShortInfo)).setVisibility(0);
((TextView) toast.getView().findViewById(R.id.TextViewShortInfo)).setText(resId);
}
toast.show();
}
}
});
}
}).start();
}
}
2. 如果只是普通的规避多次点击的需求,可以这样处理。
private void showToast(String msg)
{
if(mToast == null)
{
mToast = Toast.makeText(this,msg, 3);
mToast.show();
}
else
{
mToast.cancel();
mToast.setText(msg);
mToast.show();
}
}
1.自定义一个Toast 工具类,实现自定义布局等。
public class ToastUtils {
private static Handler handler = new Handler(Looper.getMainLooper());
private static Toast toast = null;
private static Object synObj = new Object();
public static void showToast(final Context context, final String msg,boolean flag) {
showToast(context, msg, Toast.LENGTH_SHORT,flag);
}
public static void showToast(final Context context, final int resId,boolean flag) {
showToast(context, resId, Toast.LENGTH_SHORT,flag);
}
public static void showToast(final Context context, final String msg,
final int len,final boolean flag) {
new Thread(new Runnable() {
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
synchronized (synObj) {// 同步锁
if (toast != null) {
toast.cancel();
toast.setDuration(len);
} else {
toast = Toast.makeText(context, msg, len);
View layout = LayoutInflater.from(context)
.inflate(R.layout.my_toast, null);// 自定义布局
toast.setView(layout);
}
//根据不同的情况显示不同的Toast信息
if(flag){
((TextView)toast.getView().findViewById(R.id.TextViewShortInfo)).setVisibility(4);
((TextView)toast.getView().findViewById(R.id.TextViewLongInfo)).setVisibility(0);
((TextView) toast.getView().findViewById(R.id.TextViewLongInfo)).setText(msg);
}else{
((TextView)toast.getView().findViewById(R.id.TextViewLongInfo)).setVisibility(4);
((TextView)toast.getView().findViewById(R.id.TextViewShortInfo)).setVisibility(0);
((TextView) toast.getView().findViewById(R.id.TextViewShortInfo)).setText(msg);
}
toast.show();
}
}
});
}
}).start();
}
public static void showToast(final Context context, final int resId,
final int len,final boolean flag) {
new Thread(new Runnable() {
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
synchronized (synObj) {// 同步锁
if(toast != null) {
toast.cancel();
toast.setDuration(len);
} else {
toast = Toast.makeText(context, resId, len);
View layout = LayoutInflater.from(context)
.inflate(R.layout.my_toast, null);// 自定义布局
toast.setView(layout);
}
if(flag){
((TextView)toast.getView().findViewById(R.id.TextViewShortInfo)).setVisibility(8);
((TextView)toast.getView().findViewById(R.id.TextViewLongInfo)).setVisibility(0);
((TextView) toast.getView().findViewById(R.id.TextViewLongInfo)).setText(resId);
}else{
((TextView)toast.getView().findViewById(R.id.TextViewLongInfo)).setVisibility(8);
((TextView)toast.getView().findViewById(R.id.TextViewShortInfo)).setVisibility(0);
((TextView) toast.getView().findViewById(R.id.TextViewShortInfo)).setText(resId);
}
toast.show();
}
}
});
}
}).start();
}
}
2. 如果只是普通的规避多次点击的需求,可以这样处理。
private void showToast(String msg)
{
if(mToast == null)
{
mToast = Toast.makeText(this,msg, 3);
mToast.show();
}
else
{
mToast.cancel();
mToast.setText(msg);
mToast.show();
}
}
相关文章推荐
- Toast使用小技巧
- Toast使用小技巧-防止重复创建
- 带图片的Toast及Toast使用的小技巧之Toast的封装
- Android 中Toast的使用小技巧
- Android Toast 使用小技巧
- 2.6.1 使用toast显示提示信息框
- eclipse使用小技巧
- Android studio编程中的使用小技巧
- 使用友盟分享,QQ分享成功却弹出QQ分享取消的toast问题解决
- PLSQL Developer使用小技巧
- eclipse、myeclipse使用常用的小技巧
- Composer 使用小技巧
- 数据库使用小技巧
- 微信小程序:toast的使用
- 魅族Flyme OS使用小技巧
- windows 使用小技巧
- LaTeX中table使用小技巧:全局调整字号及单元格内容自动换行
- 关于Jsoup使用的一些小技巧
- SnackBar的简单使用(类似于Toast,但比Toast更强大)
- go语言type使用小技巧