1. package com.fxb.newtest;
  2.  
  3. import com.badlogic.gdx.ApplicationListener;
  4. import com.badlogic.gdx.Gdx;
  5. import com.badlogic.gdx.Input;
  6. import com.badlogic.gdx.InputAdapter;
  7. import com.badlogic.gdx.InputMultiplexer;
  8. import com.badlogic.gdx.Input.Keys;
  9. import com.badlogic.gdx.graphics.Color;
  10. import com.badlogic.gdx.graphics.GL10;
  11. import com.badlogic.gdx.graphics.Pixmap;
  12. import com.badlogic.gdx.graphics.Texture;
  13. import com.badlogic.gdx.graphics.Pixmap.Format;
  14. import com.badlogic.gdx.graphics.g2d.BitmapFont;
  15. import com.badlogic.gdx.scenes.scene2d.Actor;
  16. import com.badlogic.gdx.scenes.scene2d.Stage;
  17. import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
  18. import com.badlogic.gdx.scenes.scene2d.ui.Skin;
  19. import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
  20. import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
  21. import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
  22. import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
  23. import com.badlogic.gdx.scenes.scene2d.ui.Window.WindowStyle;
  24. import com.badlogic.gdx.scenes.scene2d.utils.Align;
  25. import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
  26.  
  27. public class Lib005_CancelKey extends InputAdapter implements ApplicationListener{
  28.  
  29. Stage stage;
  30. Skin skin;
  31. Dialog dialog1, dialog2;
  32.  
  33. @Override
  34. public void create() {
  35. // TODO Auto-generated method stub
  36.  
  37. CreateSkin();
  38.  
  39. stage = new Stage();
  40. InputMultiplexer multiplexer = new InputMultiplexer();
  41. multiplexer.addProcessor( stage );
  42. multiplexer.addProcessor( this );
  43. Gdx.input.setInputProcessor( multiplexer );
  44. Gdx.input.setCatchBackKey( true );
  45. Gdx.input.setCatchMenuKey( true );
  46.  
  47. TextButton textbutton1 = new TextButton( "Menu", skin );
  48. TextButton textbutton2 = new TextButton( "Exit", skin );
  49.  
  50. textbutton1.addListener( new ChangeListener(){
  51. @Override
  52. public void changed(ChangeEvent event, Actor actor) {
  53. // TODO Auto-generated method stub
  54. dialog1.show(stage);
  55. }
  56. });
  57. textbutton2.addListener( new ChangeListener(){
  58. @Override
  59. public void changed(ChangeEvent event, Actor actor) {
  60. // TODO Auto-generated method stub
  61. dialog2.show(stage);
  62. }
  63. });
  64. textbutton1.setPosition( 100, 100 );
  65. textbutton2.setPosition( 200, 100 );
  66.  
  67. stage.addActor( textbutton1 );
  68. stage.addActor( textbutton2 );
  69.  
  70. }
  71.  
  72. private void CreateSkin(){
  73. skin = new Skin();
  74. Pixmap pixmap = new Pixmap( 1, 1, Format.RGBA8888 );
  75. pixmap.setColor( Color.GRAY );
  76. pixmap.fill();
  77. //draw1 = new TextureRegionDrawable( );
  78. //texture = new Texture( pixmap );
  79. skin.add( "gray", new Texture(pixmap) );
  80.  
  81. pixmap.setColor( Color.LIGHT_GRAY );
  82. pixmap.fill();
  83. skin.add( "light_gray", new Texture(pixmap) );
  84.  
  85. BitmapFont font = new BitmapFont();
  86. WindowStyle windowStyle = new WindowStyle( font, Color.GREEN, skin.getDrawable( "light_gray" ) );
  87.  
  88. LabelStyle labelStyle = new LabelStyle( font, Color.RED );
  89. ButtonStyle buttonStyle = new ButtonStyle( skin.getDrawable("gray"), skin.getDrawable("light_gray"), null );
  90. TextButtonStyle textbuttonStyle = new TextButtonStyle( skin.getDrawable("gray"), skin.getDrawable("light_gray"), null, font );
  91. skin.add( "default", buttonStyle );
  92. skin.add( "default", textbuttonStyle );
  93. skin.add( "default", windowStyle );
  94. skin.add( "default", labelStyle );
  95.  
  96. dialog1 = new Dialog( "Menu", skin, "default" ){
  97. @Override
  98. protected void result(Object object) {
  99. // TODO Auto-generated method stub
  100. //System.out.println( "Chosen" + object );
  101. if( object.toString().equals( "true" ) ){
  102. System.out.println( "Yes" );
  103. Gdx.app.exit();
  104. }else{
  105. System.out.println( "No" );
  106. }
  107. }};
  108. dialog1.text("\nMenu").button("Yes", true).button("No",false).key(Keys.ENTER, true).key(Keys.ESCAPE,false);
  109. dialog1.setTitleAlignment( Align.top );
  110.  
  111. dialog2 = new Dialog( "Exit", skin, "default" ){
  112. @Override
  113. protected void result(Object object) {
  114. // TODO Auto-generated method stub
  115. //System.out.println( "Chosen" + object );
  116. if( object.toString().equals( "true" ) ){
  117. System.out.println( "Yes" );
  118. Gdx.app.exit();
  119. }else{
  120. System.out.println( "No" );
  121. }
  122. }};
  123. dialog2.text("\nDo you want to exit?").button("Yes", true).button("No",false).key(Keys.ENTER, true).key(Keys.ESCAPE,false);
  124. dialog2.setTitleAlignment( Align.top );
  125.  
  126. }
  127.  
  128. @Override
  129. public void resize(int width, int height) {
  130. // TODO Auto-generated method stub
  131.  
  132. }
  133.  
  134. @Override
  135. public void render() {
  136. // TODO Auto-generated method stub
  137. Gdx.gl.glClearColor( 1, 1, 1, 1 );
  138. Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT );
  139.  
  140. stage.act();
  141. stage.draw();
  142. }
  143.  
  144. @Override
  145. public void pause() {
  146. // TODO Auto-generated method stub
  147.  
  148. }
  149.  
  150. @Override
  151. public void resume() {
  152. // TODO Auto-generated method stub
  153.  
  154. }
  155.  
  156. @Override
  157. public void dispose() {
  158. // TODO Auto-generated method stub
  159.  
  160. }
  161.  
  162. @Override
  163. public boolean keyDown(int keycode) {
  164. // TODO Auto-generated method stub
  165.  
  166. if( keycode == Input.Keys.BACK ){
  167. Gdx.app.log( "back", "" );
  168. dialog2.show(stage);
  169. }
  170. if( keycode == Input.Keys.MENU ){
  171. Gdx.app.log( "menu", "" );
  172. dialog1.show(stage);
  173. }
  174.  
  175. return false;
  176. }
  177.  
  178. }

libgdx开发中返回键(cancel)和菜单键(menu)的使用。

开始时需要设置Gdx.input.enableCancelKey或者Gdx.input.enableMenuKey,然后捕获到相应事件时添加事件响应即可。

libgdx退出对话框的更多相关文章

  1. Android 如何监听返回键,弹出一个退出对话框

    android 如何监听返回键点击事件,并创建一个退出对话框, 防止自己写的应用程序不小心点击退出键而直接退出.自己记录下这个简单的demo,备用. public class BackKeyTest ...

  2. Android实现“是否退出”对话框和“带图标的列表”对话框

    今天我们学习的内容是实现两种对话框(Dialog),第一种是询问是否退出对话框,另外一种是带图标的列表对话框,程序的执行效果是,我们点击button1的时候,弹出第一种对话框,我们点击button2的 ...

  3. [转]Android 如何监听返回键,弹出一个退出对话框

    本文转自:http://blog.csdn.net/sunnyfans/article/details/8094349 Android 如何监听返回键点击事件,并创建一个退出对话框, 防止自己写的应用 ...

  4. Android 自定义AlertDialog退出对话框

    Android 自定义AlertDialog退出对话框 转 https://blog.csdn.net/wkh11/article/details/53081634在项目中很多时候会出现点击返回键出现 ...

  5. quick-cocos2d-x android返回键监听并实现原生退出对话框

    这两天最终闲了一下,就顺手又把quick捡起来又学了学,一直都认为quick比cocos2dx那套lua绑定要方便很多,今天试了下android返回键的监听,还是挺好弄的,所以就有了这篇. 首先说明一 ...

  6. VB.Net中确认退出对话框的实现

    实现方法分为两大类:窗体事件和控件事件,下面就一一展示: 一.FormClosing事件(又分以下几种方法) a. Private Sub frmPractise_FormClosing(ByVal ...

  7. MFC 关于new出一个新对话框时,退出对话框内存泄漏的问题解决

    问题: 在进行点击按钮弹出对话框时,我是用了new来生成一个新的对话框,但是在新对话框关闭的时候,经过检查发现,新对话框存在内存泄漏问题. 原因: 因为使用了new,但是当时没有找到地方进行delet ...

  8. 在Android中自定义捕获Application全局异常,可以替换掉系统的强制退出对话框(很有参考价值与实用价值)

    转载自: http://blog.csdn.net/jdsjlzx/article/details/7606423

  9. android studio中退出时弹出对话框

    在app中总是不小心点击了退出怎么办?当然是加个弹出的提示框了,本人新手,就加在主界面上了 @Override public boolean onKeyDown(int keyCode, KeyEve ...

随机推荐

  1. Docker相关概念

    一.概念 ①云计算:是一种资源的服务模式,该模式可以实现随时随地,便捷按需地从可配置计算资源共享池中获取所需的资源(如网络.服务器.存储.应用及服务),资源能够快速供应并释放,大大减少了资源管理工作的 ...

  2. SqlServer为字段创建索引

    语法:CREATE [索引类型] INDEX 索引名称ON 表名(列名) 创建索引实例: 聚簇索引 create clustered index index_name on table_name (c ...

  3. docker部署nginx,并实现负载均衡。

    安装与使用 安装 nginx官网下载地址 发布版本分为 Linux 和 windows 版本. 也可以下载源码,编译后运行. 从源代码编译 Nginx 把源码解压缩之后,在终端里运行如下命令: $ . ...

  4. 关联与下钻:快速定位MySQL性能瓶颈的制胜手段

    本文根据DBAplus社群[2018年1月6日北京开源与架构技术沙龙]现场演讲内容整理而成. 讲师介绍 李季鹏 新炬网络数据库专家 专注于MySQL数据库性能管理及相关解决方案,目前主要从事MySQL ...

  5. IP解析计算机名称

    #-*- coding: UTF-8 -*- import subprocess,sys,threading reload(sys) sys.setdefaultencoding('utf-8') d ...

  6. LUA 运算笔记

    循环 比如要实现这样的一个For for(int i=10;i>1;i—) { print(i) } lua的for循环 转换成LUA for i=10,1,-1 do print(i) end ...

  7. kettle性能优化

    普通开发电脑,如果没有网络查询步骤,kettle正常的速度应该在3000~20000条/秒.如果速度在2000条/秒一下,就可能需要调优. 性能优化的方式包括如下几种: 1.通过改变开始复制的数量(针 ...

  8. 参观阿拉斯加进行产品培训[My representation]

    I  suggest to visit Alaska for product training. Alaska is one of the best places to spend a trainin ...

  9. 028、HTML 标签3表单标签插入组件

    内容:表单标签插入组件(经常使用)############################################################## form表单标签和input组件 < ...

  10. istio1.0.2配置

    项目的组件相对比较复杂,原有的一些选项是靠 ConfigMap 以及 istioctl 分别调整的,现在通过重新设计的Helm Chart,安装选项用values.yml或者 helm 命令行的方式来 ...