Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Android超实用的Toast提示框优化分享大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

前言

相信每位Android开发者都用过Toast,都知道是弹出消息的。类似于js里面的alert,C#里面的@H_712_7@mesageBox。当然android里面也有dialogdialog是有焦点的,可与用户交互。而toast是没有焦点的,时间到了自动消失,不能回应用户的交互,下面就跟大家分享下Android中Toast提示框的优化方法

先看下源码:

public class Toast { 
 
 public static final int LENGTH_SHORT = 0; 
 
 public static final int LENGTH_LONG = 1; 
 
 
 
 /** 
  * 构造一个空的toast。你必须在调动show()之前,线调用setView() 
  * @param context 参数,application或者activity都可以 
  */ 
 public Toast(Context context) { 
  ... 
  //获取系统内置的toast_y_offset常量值 
  mTN.mY = context.getresources().getDimensionPixelSize( 
    com.android.internal.R.dimen.toast_y_offset); 
  mTN.mGravity = context.getresources().getInteger( 
    com.android.internal.R.Integer.config_toastDefaultGravity); 
 } 
 
 /** 
  * 在指定的时长显示view视图 
  */ 
 public void show() { 
  //如果mNextView为空,即没有设置view 
  if (mNextView == null) { 
   throw new RuntimeException("setView must have been called"); 
  } 
 
  //通过系统服务,获取通知管理器。看来这是使用系统通知? 
  Inotificationmanager service = getservice(); 
  String pkg = mContext.getOpPackagename(); 
  TN tn = mTN; 
  tn.mNextView = mNextView; 
 
  try { 
   service.enqueueToast(pkg,tn,mDuration); 
  } catch (remoteexception E) { 
   // Empty 
  } 
 } 
 
 
 /** 
  * 关闭一个正在显示的toast,或者取消一个显示的toast. 
  * 通常你不必调用它,在适当的时长后它会自动消失的。 
  */ 
 public void cancel() { 
  mTN.hide(); 
 
  try { 
   getservice().cancelToast(mContext.getPackagename(),mTN); 
  } catch (remoteexception E) { 
   // Empty 
  } 
 } 
 
 /** 
  * 设置toast显示的视图内容,不单单是黑色的界面,你可以自己决定显示什么 
  * @see #getView 
  */ 
 public void setView(View view) { 
  mNextView = view; 
 } 
 
 /** 
  * 设置时长,只能是下面这两个常量值,没什么卵用 
  * @see #LENGTH_SHORT 2000毫秒 
  * @see #LENGTH_LONG 3500毫秒 
  */ 
 public void setDuration(@Duration int duration) { 
  mDuration = duration; 
 } 
 
 
 /** 
  * 设置view的外间距,不用多说. 
  * 
  * @param horizontalMargin The horizontal margin,in percentage of the 
  *  container width,between the container's edges and the 
  *  notification 
  * @param verticalMargin The vertical margin,in percentage of the 
  *  container height,between the container's edges and the 
  *  notification 
  */ 
 public void setMargin(float horizontalMargin,float verticalMargin) { 
  mTN.mHorizontalMargin = horizontalMargin; 
  mTN.mVerticalMargin = verticalMargin; 
 } 
 
 
 /** 
  * 设置notification在屏幕中的方位,大家都知道.上中下左中右什么的都有 
  * @see android.view.Gravity 
  * @see #getGravity 
  */ 
 public void setGravity(int gravity,int xOffset,int yOffset) { 
  mTN.mGravity = gravity; 
  mTN.mX = xOffset; 
  mTN.mY = yOffset; 
 } 
 
 
 
 /** 
  * 构造一个只包含一个TextView的标准toast对象 
  * 
  * @param context 通常是application或者activity对象 
  * @param text  用于显示的文本,可以是formatted text. 
  * @param duration 显示时长. LENGTH_SHORT或LENGTH_LONG 
  * 
  */ 
 public static Toast makeText(Context context,CharSequence text,@Duration int duration) { 
  Toast result = new Toast(context); 
 
  LayoutInflater inflate = (LayoutInflater) 
    context.getSystemservice(Context.LAYOUT_INFLATER_serviCE); 
 
  //包含了一个认的TextView,这个textview的布局位置在 
 
com.android.internal.R.layout.transient_notification,可以去查看下内容 
  View v = inflate.inflate(com.android.internal.R.layout.transient_notification,null); 
  TextView tv = (TextView)v.findViewById(com.android.internal.R.id.@R_674_8798@gE); 
  tv.setText(text); 
   
  result.mNextView = v; 
  result.mDuration = duration; 
 
  return result; 
 } 
 
 
 
 /** 
  * 更新通过makeText()方法创建出来的toast对象显示的文本内容 
  * @param s 待显示的新文本内容. 
  */ 
 public void setText(CharSequence s) { 
  if (mNextView == null) { 
   throw new RuntimeException("This Toast was not created with Toast.makeText()"); 
  } 
 
  /** 
   * 看来com.android.internal.R.layout.transient_notification布局里面的唯一的 
   * TextView标签的id是R.id.@R_674_8798@ge。拿到这个textview,设置新文本内容 
   */ 
  TextView tv = (TextView) mNextView.findViewById(com.android.internal.R.id.@R_674_8798@gE); 
  if (tv == null) { 
   throw new RuntimeException("This Toast was not created with Toast.makeText()"); 
  } 
  tv.setText(s); 
 } 
 
 
 static private Inotificationmanager getservice() { 
  if (sservice != null) { 
   return sservice; 
  } 
  //获取远程的通知服务 
  sservice = Inotificationmanager.stub.asInterface(serviceManager.getservice("notification")); 
  return sservice; 
 } 
 
 
 //TN是一个瞬态通知的子类,里面包含显示和隐藏两个任务对象 
 private static class TN extends ITransientNotification.stub { 
  final Runnable mShow = new Runnable() { 
   @Override 
   public void run() { 
    handleShow(); 
   } 
  }; 
 
  final Runnable mHide = new Runnable() { 
   @Override 
   public void run() { 
    handleHide(); 
    // Don't do this in handleHide() because it is also invoked by handleShow() 
    mNextView = null; 
   } 
  }; 
  
  //出现Handler了哦 
  final Handler mHandler = new Handler(); 
 
 
  /** 
   * 调度handleShow任务到执行线程中 
   */ 
  @Override 
  public void show() { 
   if (LOCALLOGV) Log.v(tag,"SHOW: " + this); 
   //handler发送异步任务了 
   mHandler.post(mShow); 
  } 
 
 
  /** 
   * 同上 
   */ 
  @Override 
  public void hide() { 
   if (LOCALLOGV) Log.v(tag,"HIDE: " + this); 
   mHandler.post(mHidE); 
  } 
 
  //... 
 } 
 
} @H_616_30@

通过上面的源码解读,了解到有远程通知handler异步任务等信息,不多说,自己看。

重点是toast的用法

1、直接@L_668_8@makeText静态方法即可,返回的是Toast对象,最后别忘了调用show方法显示

Toast.makeText(context,text,duration).show(); @H_616_30@


Toast toast = Toast.makeText(context,duration); 
pre name="code" class="html"> toast.setView(view); 
toast.setText(s); 
toast.setGravity(gravity,xOffset,yOffset); 
toast.setDuration(duration); 
toast.show();@H_616_30@

2、还可以使用New的方式创建,别忘了setView()方法

Toast toast = new Toast(); 
toast.setView(view); 
toast.setText(s); 
toast.setGravity(gravity,yOffset); 
toast.setDuration(duration); 
toast.show(); @H_616_30@

以上这些都不值得一提,很简单。

在开发过程中,有这样的需求:在项目总,我们偷懒,想连串toast出多个变量的值或者其他任务,可在操作手机时直观可见。问题来了,弹出是无论我们的操作有多快,这些toast内容都是一个跟着一个显示,没办法快进。哪怕我们玩完了,退出了app,它还在弹。怎么办?有没有办法让toast内容与我们的操作同步,快速反应?

public class T { 
 private static Toast toast; 
 
 public static void show(Context context,String msg) { 
  if (toast == null) { 
   toast = Toast.makeText(context,msg,Toast.LENGTH_SHORT); 
  } else { 
   toast.setText(msg); 
  } 
  toast.show(); 
 } 
} @H_616_30@

单例模式,每次创建toast调用这个类的show方法Toast的生命周期从show开始,到自己消失或者cancel为止。如果正在显示,则修改显示内容,你持有这个对象的引用,当然可以修改显示内容了。若每次你都@H_712_7@makeText或者new一个toast对象,即每次通过handler发送异步任务,调用远程通知服务显示通知,当然是排队等待显示了。

结束语

以上就是Android中Toast提示框优化的全部内容,希望对大家开发Android能有所帮助,如果有大家有疑问可以留言交流。

大佬总结

以上是大佬教程为你收集整理的Android超实用的Toast提示框优化分享全部内容,希望文章能够帮你解决Android超实用的Toast提示框优化分享所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。