防止Toast消息在触摸时被取消

4

我正在开发一个游戏,其中需要显示toast消息来提供指示。

问题在于,只要用户触摸屏幕,toast消息就会被取消。

我该如何防止onTouch事件取消toast消息?

创建toast消息所使用的代码:

    Toast.makeText(context, toastMsg, toastLength);

感谢您的帮助!!!

14
为什么你认为在首次使用时使用“Toast”提示是一个好主意? - CommonsWare
你是否在 Toast 消息上调用了 show() 方法?如果没有调用,它将不会弹出。 - Andrew Schuster
虽然我永远不会建议这样做,但你可以让线程睡眠一段时间,以便它不处于“活动”状态。再次强调,我绝不建议让线程睡眠,这只是一个超级hack(可能的)答案。 - zgc7009
你可以在用户界面中制作一个小的信息框,向用户提供信息。这对用户更好。 - Marco Acierno
我很惊讶你这个问题没有更多的赞。在搜索后没有获得相关结果,我本来也要问同样的事情。我认为这对游戏开发者来说应该是一个巨大的问题,对我来说确实如此。 - Androidcoder
不要使用toast来做这件事。 - EpicPandaForce
3个回答

1

这是一个带有计时器的透明对话框,我经常在我的辅助中使用它来提供指导或说明。

public static void Toaster(final Context ctx, final String text) {
    final Dialog dialog = new Dialog(ctx);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setFlags(
            android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN,
            android.view.WindowManager.LayoutParams.FLAG_FULLSCREEN);
    dialog.getWindow().setBackgroundDrawable(
            new ColorDrawable(Color.TRANSPARENT));
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.guide);
    TextView guide = (TextView) dialog.findViewById(R.id.g_text);
    guide.setText(text);
    Button buy = (Button) dialog.findViewById(R.id.gotit);
    buy.setVisibility(View.INVISIBLE);
    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
    lp.copyFrom(dialog.getWindow().getAttributes());
    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
    lp.height = WindowManager.LayoutParams.MATCH_PARENT;
    dialog.show();
    dialog.getWindow().setAttributes(lp);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            dialog.dismiss();
        }
    }, 2000);
}

这里是xml文件。
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center|center"
        android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:gravity="center"
        android:textColor="#ffffff"        
        android:id="@+id/g_text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:gravity="center|bottom"
        android:orientation="vertical" >
    <Button
        android:id="@+id/gotit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:textColor="#2f72da"
        android:layout_gravity="bottom"
        android:text="@string/got_it" />
    </LinearLayout>
</LinearLayout>

并且只需使用

即可调用它。
Toaster(YourClassName.this,"Your Text");

0

你可以使用警告按钮

 AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("Warning");
            builder.setMessage("this is a question?");
            builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                     //do sth

                    dialog.dismiss();
                }

            });

            builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    //do sth else
                    dialog.dismiss();
                }
            });

            alert = builder.create();
            alert.show();

0

我是一名游戏开发者,所以这对我来说是一个很大的问题。Toast不仅会在屏幕点击时消失,而且还容易受到屏幕拖动的影响。OpenGL没有文本支持,因此如果没有Toast,我最终不得不创建和显示任何临时文本的位图。这对于多语言支持尤其耗费时间和内存。

我的解决方法是使用全局变量来存储Toast字符串,将一个Long变量设置为它最初启动的系统时间,然后在我的downtouch接口代码中重新显示Toast,如果在3500毫秒(Toast.LENGTH_LONG的持续时间)之前发生任何downtouch,则重新显示。

for (int downtouch = 0; downtouch < downTouches.length; downtouch++) {
  ...
   if(downtouch==0&&System.currentTimeMillis()<toastmessagestart+3500) {//the first of any simultaneous touches
            ((Activity) getContext()).runOnUiThread(new Runnable() {
                Toast toast;
                public void run() {
                    if(System.currentTimeMillis()<toastmessagestart+1500){//duration Toast.LENGTH_LONG - duration Toast.LENGTH_SHORT
                        toast = Toast.makeText(getContext(), globaltoaststring, Toast.LENGTH_LONG);
                    }else{
                        toast = Toast.makeText(getContext(), globaltoaststring, Toast.LENGTH_SHORT);
                    }
                    toast.show();
                }
            });
   }

这个 hack 并不完美,因为烤面包的持续时间会根据屏幕交互而有所变化,但至少会持续 3500 毫秒。此外可能会出现间歇性的淡入淡出,但意外地并不多。


如果你的游戏有任何价值,你可能想要一些应用内UI来处理这些内容,而不是使用toast提示。 - EpicPandaForce

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接