java - 安卓开发 : AlertDialog with SurfaceView

标签 java android

我正在开发一款 Android 游戏,我有 SurfaceView 来渲染游戏,我想让它在玩家输或赢时弹出一个 AlertDialog,然后重新启动关卡或进行其他操作。

基本上我有两个问题:

  1. 如何将 AlertDialogs 与 SurfaceViews 一起使用?我必须将其放入layout.xml 中还是将其编码到UI 部分或游戏线程部分中?

  2. 有没有一种方法可以“重新启动”一项 Activity ,这样它就不会导致新的 Activity 只是以最初给出的相同“Intent ”重新启动当前 Activity ?

谢谢!

最佳答案

您可以在任何事件上使用 this.Start(); 来重新启动 Activity 中的线程,这就是第二个问题的答案。

或者你可以使用

if(maze.isGameComplete()) {
   AlertDialog.Builder builder = new AlertDialog.Builder(context);
   builder.setTitle(context.getText(R.string.finished_title));
   LayoutInflater inflater = context.getLayoutInflater();
   View view = inflater.inflate(R.layout.finish, null);
   builder.setView(view);
   View closeButton =view.findViewById(R.id.closeGame);

   closeButton.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View clicked) {
         if(clicked.getId() == R.id.closeGame) {
            context.finish();
         }
      }
   });

   AlertDialog finishDialog = builder.create();
   finishDialog.show();
}

关于java - 安卓开发 : AlertDialog with SurfaceView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6950827/

相关文章:

java - 使用 PostgreSQL 和 Spring Boot 时是否有规则禁止将其实体类命名为 "User"?

android - RecyclerView Observer, fragment 未更新数据

java - Android/XML : RelativeLayout design

java - 通过 gradle 构建公开依赖项

android - 使用 GSON 解析 JSON 时出错

android - 同时缩小和淡入 imageView

android - 在安卓屏幕上显示完整的世界地图

java - 如何检测 FragmentPagerAdapter 显示的第一页或最后一页?

java - Spring 安全: Handling BOTH web app security and web service security

java - 写入外部变量时,将内部循环与流并行化