我正在开发一款游戏,在其中显示吐司消息以作指示。
问题是,用户一旦触摸屏幕,吐司消息就会被取消。
如何防止onTouch事件取消Toast消息?
用于创建吐司消息的代码:
Toast.makeText(context, toastMsg, toastLength);
感谢您的帮助!
您可以使用警报按钮
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();
这里是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");
我是游戏开发者,所以对我来说这是一个大问题。吐司不仅会在屏幕上消失,而且还容易受到屏幕拖曳的影响,而我的玩家一直在不断移动。 OpenGL不提供文本支持,因此,在没有举杯的情况下,我最终不得不创建并显示要显示的任何文本的位图。这非常浪费时间和内存,特别是对于多语言支持。