libGDX Alert Dialog

前端 未结 3 1056
花落未央
花落未央 2020-12-16 03:36

I have used code below:

 AlertDialog.Builder bld;

 if (android.os.Build.VERSION.SDK_INT <= 10) {
     //With default theme looks perfect:
     bld = new          


        
3条回答
  •  一整个雨季
    2020-12-16 04:32

    In libgdx you should use a scene2d dialog instead of the native Android DialogInterface. Below is how you would add a completely skinned dialog to the stage in libgdx with custom button images and background image. You would just need to substitute your own background and button image textures and font, then call quitGameConfirm() when you're ready to display the dialog...

    import com.badlogic.gdx.scenes.scene2d.ui.Dialog;
    
    public void quitGameConfirm() {
    
        LabelStyle style = new LabelStyle(_fontChat, Color.WHITE);
        Label label1 = new Label("Are you sure that you want to exit?", style);
        label1.setAlignment(Align.center);
        //style.font.setScale(1, -1);
        style.fontColor = Color.WHITE;
    
        Skin tileSkin = new Skin();
        Texture tex = new Texture(myButtontexture);
        tex.setFilter(TextureFilter.Linear, TextureFilter.Linear);
        tileSkin.add("white", tex);
        tileSkin.add("default", new BitmapFont());
    
        TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
        textButtonStyle.up = tileSkin.newDrawable("white");
        textButtonStyle.down = tileSkin.newDrawable("white", Color.DARK_GRAY);
        textButtonStyle.checked = tileSkin.newDrawable("white",
                Color.LIGHT_GRAY);
        textButtonStyle.over = tileSkin.newDrawable("white", Color.LIGHT_GRAY);
        textButtonStyle.font = _myTextBitmapFont;
        textButtonStyle.font.setScale(1, -1);
        textButtonStyle.fontColor = Color.WHITE;
        tileSkin.add("default", textButtonStyle);
    
        TextButton btnYes = new TextButton("Exit", tileSkin);
        TextButton btnNo = new TextButton("Cancel", tileSkin);
    
        // /////////////////
        Skin skinDialog = new Skin(Gdx.files.internal("data/uiskin.json"));
        final Dialog dialog = new Dialog("", skinDialog) {
            @Override
            public float getPrefWidth() {
                // force dialog width
                // return Gdx.graphics.getWidth() / 2;
                return 700f;
            }
    
            @Override
            public float getPrefHeight() {
                // force dialog height
                // return Gdx.graphics.getWidth() / 2;
                return 400f;
            }
        };
        dialog.setModal(true);
        dialog.setMovable(false);
        dialog.setResizable(false);
    
        btnYes.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y,
                    int pointer, int button) {
    
                // Do whatever here for exit button
    
                _parent.changeState("StateMenu");
                dialog.hide();
                dialog.cancel();
                dialog.remove();                
    
                return true;
            }
    
        });
    
        btnNo.addListener(new InputListener() {
            @Override
            public boolean touchDown(InputEvent event, float x, float y,
                    int pointer, int button) {
    
                //Do whatever here for cancel
    
                dialog.cancel();
                dialog.hide();
    
                return true;
            }
    
        });
    
        TextureRegion myTex = new TextureRegion(_dialogBackgroundTextureRegion);
        myTex.flip(false, true);
        myTex.getTexture().setFilter(TextureFilter.Linear, TextureFilter.Linear);
        Drawable drawable = new TextureRegionDrawable(myTex);
        dialog.setBackground(drawable);
    
        float btnSize = 80f;
        Table t = new Table();
        // t.debug();
    
        dialog.getContentTable().add(label1).padTop(40f);
    
        t.add(btnYes).width(btnSize).height(btnSize);
        t.add(btnNo).width(btnSize).height(btnSize);
    
        dialog.getButtonTable().add(t).center().padBottom(80f);
        dialog.show(stage).setPosition(
                (MyGame.VIRTUAL_WIDTH / 2) - (720 / 2),
                (MyGame.VIRTUAL_HEIGHT) - (MyGame.VIRTUAL_HEIGHT - 40));
    
        dialog.setName("quitDialog");
        stage.addActor(dialog);
    
    }
    

    enter image description here

提交回复
热议问题