我正在开发一个游戏,其中需要显示toast消息来提供指示。
问题在于,只要用户触摸屏幕,toast消息就会被取消。
我该如何防止onTouch事件取消toast消息?
创建toast消息所使用的代码:
Toast.makeText(context, toastMsg, toastLength);
感谢您的帮助!!!
我正在开发一个游戏,其中需要显示toast消息来提供指示。
问题在于,只要用户触摸屏幕,toast消息就会被取消。
我该如何防止onTouch事件取消toast消息?
创建toast消息所使用的代码:
Toast.makeText(context, toastMsg, toastLength);
这是一个带有计时器的透明
对话框
,我经常在我的辅助类
中使用它来提供指导或说明。
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");
你可以使用警告按钮
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();
我是一名游戏开发者,所以这对我来说是一个很大的问题。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 毫秒。此外可能会出现间歇性的淡入淡出,但意外地并不多。
Toast
消息上调用了show()
方法?如果没有调用,它将不会弹出。 - Andrew Schuster