解决Toast不显示问题 Toast invisible to user . go ahead

来源:互联网 发布:node supervisor 编辑:程序博客网 时间:2024/06/08 00:37


Toast invisible to user 或 go ahead

关于Toast在一些手机和Pad上不显示的解决方法:


1、

Toast.makeText(Context, "这是一个Toast", Toast.LENGTH_SHORT);

如果你的调用方法为上述则只要在后面加上一个show()方法就可以解决。如:

Toast.makeText(Context, "这是一个Toast", Toast.LENGTH_SHORT).show();

如果没有解决那么请往下看:

2、请检查Context是否为空。如果不为空请往下看:

3、如果在子线程中调用Toast不会显示。因为Toast并没有在UI线程用调用;解决方法:

Looper.prepare();Toast.makeText(MainActivity.this, "这是一个Toast222", Toast.LENGTH_SHORT).show();Looper.loop();

或者通过Handler发送消息:如:

new Thread(new Runnable() {@Overridepublic void run() {mHandler.sendEmptyMessage(1);}}).start();Handler mHandler = new Handler(){public void dispatchMessage(android.os.Message msg) {switch (msg.what) {case 1:Toast.makeText(MainActivity.this, "这是一个toast", Toast.LENGTH_SHORT).show();break;default:break;}};};

这样同样可以显示。

4、如果上面都没有解决你的Toast显示问题。那么可能就是你运行的那个系统隐藏了你的toast系统显示。会报出From com.....(包名), invisible to user.我就在小米pad上碰到过还有其他的一些手机。这样怎么解决类?大家去看看Toast的源码。我们其实自己可以自己写一个Toast。这样就不管你系统怎么阻止我显示都一样可以显示。详细代码看下面。


package com.example.toasttest;import android.content.Context;import android.content.res.Resources;import android.graphics.Color;import android.graphics.PixelFormat;import android.os.Handler;import android.view.Gravity;import android.view.View;import android.view.WindowManager;import android.widget.LinearLayout;import android.widget.LinearLayout.LayoutParams;import android.widget.TextView;public class XToast {private Context mContext;private WindowManager wm;private int mDuration;private View mNextView;public static final int LENGTH_SHORT = 1500;public static final int LENGTH_LONG = 3000;public XToast(Context context) {mContext = context.getApplicationContext();wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);}public static XToast makeText(Context context, CharSequence text,int duration) {XToast result = new XToast(context);LinearLayout ll = new LinearLayout(context);ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));ll.setBackgroundResource(R.drawable.bg_toast);TextView tv = new TextView(context);LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);params.setMargins(dip2px(context, 15), dip2px(context, 10), dip2px(context, 15), dip2px(context, 10));tv.setLayoutParams(params );tv.setText(text);tv.setTextColor(Color.parseColor("#ff0000"));ll.addView(tv);result.mNextView = (View)ll;result.mDuration = duration;return result;}public static XToast makeText(Context context, int resId, int duration)throws Resources.NotFoundException {return makeText(context, context.getResources().getText(resId),duration);}public void show() {if (mNextView != null) {WindowManager.LayoutParams params = new WindowManager.LayoutParams();params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;params.height = WindowManager.LayoutParams.WRAP_CONTENT;params.width = WindowManager.LayoutParams.WRAP_CONTENT;params.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON;params.format = PixelFormat.TRANSLUCENT;params.windowAnimations = R.style.Animation_Toast;params.y = dip2px(mContext, 64);params.type = WindowManager.LayoutParams.TYPE_TOAST;wm.addView(mNextView, params);new Handler().postDelayed(new Runnable() {@Overridepublic void run() {if (mNextView != null) {wm.removeView(mNextView);mNextView = null;wm = null;}}}, mDuration);}}public static int dip2px(Context context, float dipValue) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (dipValue * scale + 0.5f);}}

在这个里面我用的是Toast背景是自己写的一个drawbale

bg_toast如下:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >    <corners         android:radius="5dp"        />    <gradient android:startColor="#bb000000"        android:centerColor="#bb000000"        android:endColor="#bb000000"/></shape>
关于这个背景大家可以根据自己的需求随意修改,也可以让美工切图。都可以的。

大家注意看那个show()方法。里面用到了一个动画。动画是一个Alpha动画很简单。演示就只用了这个。

如下:在style中加入

<style name="Animation_Toast" parent="@android:style/Animation">        <item name="android:windowEnterAnimation">@anim/toast_enter</item>        <item name="android:windowExitAnimation">@anim/toast_exit</item></style>

然后再res中建立anim文件夹最后在anim中添加toast进入和退出的动画

toast_enter动画

<?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="@android:integer/config_longAnimTime"    android:fromAlpha="0.0"    android:interpolator="@android:interpolator/decelerate_quad"android:toAlpha="1.0" />


toast_exit动画

<?xml version="1.0" encoding="utf-8"?><alpha xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="@android:integer/config_longAnimTime"    android:fromAlpha="1.0"    android:interpolator="@android:interpolator/accelerate_quad"    android:toAlpha="0.0" />

其他的动画大家自己可以根据自己的要求加。在小米中的Toast动画是一个translate以后再用的时候跟普通的那个Toast一样用。比如:

在主线程中:XToast.makeText(MainActivity.this, "这是我的toast2222", XToast.LENGTH_SHORT).show();在子线程中:Looper.prepare();XToast.makeText(MainActivity.this, "这是我的toast2222", XToast.LENGTH_SHORT).show();Looper.loop();

这样就可以了。 基本能解决.如果还有更好的方法欢迎留言。












7 0