package com.fxb.newtest;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputMultiplexer;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Button.ButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Label.LabelStyle;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton.TextButtonStyle;
import com.badlogic.gdx.scenes.scene2d.ui.Window.WindowStyle;
import com.badlogic.gdx.scenes.scene2d.utils.Align;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener; public class Lib005_CancelKey extends InputAdapter implements ApplicationListener{ Stage stage;
Skin skin;
Dialog dialog1, dialog2; @Override
public void create() {
// TODO Auto-generated method stub CreateSkin(); stage = new Stage();
InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor( stage );
multiplexer.addProcessor( this );
Gdx.input.setInputProcessor( multiplexer );
Gdx.input.setCatchBackKey( true );
Gdx.input.setCatchMenuKey( true ); TextButton textbutton1 = new TextButton( "Menu", skin );
TextButton textbutton2 = new TextButton( "Exit", skin ); textbutton1.addListener( new ChangeListener(){
@Override
public void changed(ChangeEvent event, Actor actor) {
// TODO Auto-generated method stub
dialog1.show(stage);
}
});
textbutton2.addListener( new ChangeListener(){
@Override
public void changed(ChangeEvent event, Actor actor) {
// TODO Auto-generated method stub
dialog2.show(stage);
}
});
textbutton1.setPosition( 100, 100 );
textbutton2.setPosition( 200, 100 ); stage.addActor( textbutton1 );
stage.addActor( textbutton2 ); } private void CreateSkin(){
skin = new Skin();
Pixmap pixmap = new Pixmap( 1, 1, Format.RGBA8888 );
pixmap.setColor( Color.GRAY );
pixmap.fill();
//draw1 = new TextureRegionDrawable( );
//texture = new Texture( pixmap );
skin.add( "gray", new Texture(pixmap) ); pixmap.setColor( Color.LIGHT_GRAY );
pixmap.fill();
skin.add( "light_gray", new Texture(pixmap) ); BitmapFont font = new BitmapFont();
WindowStyle windowStyle = new WindowStyle( font, Color.GREEN, skin.getDrawable( "light_gray" ) ); LabelStyle labelStyle = new LabelStyle( font, Color.RED );
ButtonStyle buttonStyle = new ButtonStyle( skin.getDrawable("gray"), skin.getDrawable("light_gray"), null );
TextButtonStyle textbuttonStyle = new TextButtonStyle( skin.getDrawable("gray"), skin.getDrawable("light_gray"), null, font );
skin.add( "default", buttonStyle );
skin.add( "default", textbuttonStyle );
skin.add( "default", windowStyle );
skin.add( "default", labelStyle ); dialog1 = new Dialog( "Menu", skin, "default" ){
@Override
protected void result(Object object) {
// TODO Auto-generated method stub
//System.out.println( "Chosen" + object );
if( object.toString().equals( "true" ) ){
System.out.println( "Yes" );
Gdx.app.exit();
}else{
System.out.println( "No" );
}
}};
dialog1.text("\nMenu").button("Yes", true).button("No",false).key(Keys.ENTER, true).key(Keys.ESCAPE,false);
dialog1.setTitleAlignment( Align.top ); dialog2 = new Dialog( "Exit", skin, "default" ){
@Override
protected void result(Object object) {
// TODO Auto-generated method stub
//System.out.println( "Chosen" + object );
if( object.toString().equals( "true" ) ){
System.out.println( "Yes" );
Gdx.app.exit();
}else{
System.out.println( "No" );
}
}};
dialog2.text("\nDo you want to exit?").button("Yes", true).button("No",false).key(Keys.ENTER, true).key(Keys.ESCAPE,false);
dialog2.setTitleAlignment( Align.top ); } @Override
public void resize(int width, int height) {
// TODO Auto-generated method stub } @Override
public void render() {
// TODO Auto-generated method stub
Gdx.gl.glClearColor( 1, 1, 1, 1 );
Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT ); stage.act();
stage.draw();
} @Override
public void pause() {
// TODO Auto-generated method stub } @Override
public void resume() {
// TODO Auto-generated method stub } @Override
public void dispose() {
// TODO Auto-generated method stub } @Override
public boolean keyDown(int keycode) {
// TODO Auto-generated method stub if( keycode == Input.Keys.BACK ){
Gdx.app.log( "back", "" );
dialog2.show(stage);
}
if( keycode == Input.Keys.MENU ){
Gdx.app.log( "menu", "" );
dialog1.show(stage);
} return false;
} }

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

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

最新文章

  1. 理解加密算法(三)——创建CA机构,签发证书并开始TLS通信
  2. 基于 React.js + Redux + Bootstrap 的 Ruby China 示例 (转)
  3. 如何在string.Format()方法中输出大括号
  4. 关于PHP堆栈与列队
  5. POJ2157Maze[DFS !]
  6. 问题解决(一)在ipad上通过safari浏览文档
  7. [CLR via C#]19. 可空值类型
  8. Linux常用指令---kill | killall(终止进程)
  9. HDU 2157
  10. Dijkstra最短路径算法
  11. [转载]C#时间函数
  12. JQuery给元素绑定click事件多次执行的解决方法
  13. Code digest
  14. VS生产的编辑方法和编辑窗体
  15. 说说new 和 malloc()
  16. d3里的比例尺
  17. solidity 汇编语言问题——内存数据mload时为什么从第32位开始
  18. 解决Access查询不区分大小写问题
  19. [BZOJ 1568][JSOI2008]Blue Mary开公司
  20. 执行了‘“npm install mysql"

热门文章

  1. 解决ci框架php发送邮件附件中文乱码问题
  2. python 中* 和**的作用
  3. (转)在.net中检索HTTP请求
  4. deep learning自学知识积累笔记
  5. jqGrid删除单条和多条数据
  6. jetty和tomcat比较
  7. python 多进程和子进程1
  8. 调用webservice 的时候没法输入参数
  9. STM32学习之路-LCD(2)
  10. 异常处理(try...catch...final 和 throw , throws)