简体   繁体   中英

Android - Issue with Dialog Boxes/ clicking of buttons

I have a game where the user is moving around on a canvas and can interact with an item in this scenario a dog which triggers a battle.

This loads up a dialog giving the user the option to fight or run away. The user clicks fight, which then brings up another dialog, this displays some text (your details and the dogs details), and underneath that text there is an attack buttons.

When you click the attach button a few things happen (not relvent), which essentially mean the user and dog details change (eg DogHP drops to 18).

So my question is:

  • The first dialog box that I load has dynamic information 'Dog HP'.
  • After the attack button is clicked then that information is changed.
  • Therefore, I need to update the first dialog box with the new information which I am currently displaying using builder.setMessage()
  • eg orginal HP = 20 ... attack button clicked ... new HP = 10

Please see some code snippet below, any ideas or guidence would be much appreciated:

Code Snippet:

void Fight()
{
    final int DogHP = 25;
    final int DogAtk = 15;
    final int DogDef = 5;
    final int DogSpeed = 20;

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setTitle(context.getText(R.string.monster_dog_title));
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.fight, null);
    builder.setView(view);
    final AlertDialog DogBattle = builder.create();

    View FightBtn = view.findViewById(R.id.dog_fight);
    FightBtn.setOnClickListener(new OnClickListener()
    {
        boolean attacked = false;
        @Override
        public void onClick(View clicked)
        {
            if(clicked.getId() == R.id.dog_fight)
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle(context.getText(R.string.monster_fight));
                builder.setMessage("Your Current Battle Details:\n" + "HP = " + UserHP + "  Def = " + UserDef + "  Atk = " + UserAtk + "\n\n" + "Your Enemy:\n" + "HP = " + DogHP + "  Def = " + DogDef + "  Atk = " + DogAtk);

                LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View view = inflater.inflate(R.layout.monsterbattle, null);
                builder.setView(view);
                final AlertDialog BattleRun = builder.create();
                BattleRun.show();
                View LeaveBattle = view.findViewById(R.id.Attack);
                LeaveBattle.setOnClickListener(new OnClickListener()
                {
                    @Override
                    public void onClick(View clicked)
                    {
                        if(clicked.getId() == R.id.Attack)
                        {
                            // SOME CODE GOES HERE
                        }
                    }
                });
            }
        }
    });
}

Thanks in Advance for any help.

I haven't tried this, but I believe that the id of the text field in the dialog is android.R.id.custom, so you should be able to do something like:

((TextView)dialog.findViewById(android.R.id.custom)).setText(newTextToShow)

A better approach, though, might be to use a custom dialog.

我决定完全废弃上面的想法,因为在对话框中发生了太多事情,最终创建了一个新类,同时使用对象而不是变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM