美文网首页
Toast分析

Toast分析

作者: 瀚海来客 | 来源:发表于2019-11-14 16:52 被阅读0次

    声明:
    【转自】https://cloud.tencent.com/developer/article/1035830

    自定义Toast

    1.自定义Toast相当于自己做了makeText()方法的工作
    源码位置:frameworks/base/core/java/Android/widght/Toast.java
    Toast#makeText()

    /**
         * Make a standard toast to display using the specified looper.
         * If looper is null, Looper.myLooper() is used.
         * @hide
         */
        public static Toast makeText(@NonNull Context context, @Nullable Looper looper,
                @NonNull CharSequence text, @Duration int duration) {
            Toast result = new Toast(context, looper);
    
            LayoutInflater inflate = (LayoutInflater)
                    context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
            TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
            tv.setText(text);
    
            result.mNextView = v;
            result.mDuration = duration;
    
            return result;
        }
    

    2.show()方法中内容

    灵魂三问

    • 通过getService()怎么就获得一个INotificationManager对象?
    • TN类是个什么鬼?
    • 方法最后只有一个service.enqueueToast(),显示和隐藏在哪里?
    public void show() {
            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分析

          本文链接:https://www.haomeiwen.com/subject/abgrictx.html