防止吐司消息在触摸时取消

时间:2014-05-27 19:40:36

标签: android ontouchevent android-toast

我正在开发一款游戏,其中我正在显示Toast消息以获取指示。

问题是一旦用户触摸屏幕,吐司信息就会被取消。

如何阻止onTouch事件取消Toast消息?

用于创建Toast消息的代码:

    Toast.makeText(context, toastMsg, toastLength);

感谢您的帮助!!!

4 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

这是一个transparent dialog,其中包含我经常在帮助者class中使用的计时器,以提供指导或说明。

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");

答案 2 :(得分:0)

我是游戏开发者,所以这是一个大问题。我的解决方法是简单地重复相同的Toast消息活动两到三次。当用户通过点击屏幕无意中杀死第一个消息时,将立即出现在Toast队列中的下一个相同消息。这也是使消息显示的时间长于Toast.LENGTH_LONG给出的时间的方法。

答案 3 :(得分:0)

我是游戏开发者,所以这对我来说是个大问题。吐司不仅在屏幕上消失,而且还容易受到屏幕拖动的影响。 OpenGL不提供文本支持,因此,在没有举杯的情况下,我最终不得不创建并显示要显示的任何临时文本的位图。这非常浪费时间和内存,尤其是对于多语言支持。

我的技巧是为Toast字符串使用全局变量,将Long变量设置为它最初启动的系统时间,然后如果在3500毫秒之前发生任何降落操作,则在我的DownTouch界面代码中重新显示Toast(持续时间Toast.LENGTH_LONG )已通过。

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();
                }
            });
   }

这种破解并不完美,因为吐司的感知长度会因屏幕交互而有所不同,但至少需要3500毫秒。另外,可能会出现间歇性的衰落,但出人意料的是,它不多。