4

我正在开发一个游戏,我在其中显示 toast 消息以获取说明。

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

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

用于创建 toast 消息的代码:

    Toast.makeText(context, toastMsg, toastLength);

谢谢你的帮助!!!

4

3 回答 3

1

这是一个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");
于 2014-05-28T11:07:28.653 回答
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();
于 2014-05-28T10:44:53.643 回答
0

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

我的技巧是为 toast 字符串使用全局变量,将 Long 变量设置为它最初启动的系统时间,然后如果在 3500 毫秒(持续时间 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();
                }
            });
   }

这个技巧并不完美,因为 toast 的感知长度会根据屏幕交互而有所不同,但至少为 3500 毫秒。也可能会出现间歇性褪色,但令人惊讶的是不多。

于 2019-10-30T22:33:00.233 回答