lundi 2 mars 2020

Need help getting my "Play" button to send the user to their current level

I'm very new to app development (4 days in), I'm creating a logic math game with individual levels. Level1 is sending SharedPreferences key "currentlevel" with a string value of "1". The code I have below works, the Play button sends me to Level2 if I've already beaten Level1.

My problem is that I have to double-click the Play button instead of the normal single click. I suspect its because I have 2 View.OnClickListeners, but deleting one of them causes it to not work at all. Any help in getting this resolved is greatly appreciated.

    Button button1 = (Button) findViewById(R.id.playbutton);
  final Button button3 = (Button) findViewById(R.id.playbutton);

        SharedPreferences result = getSharedPreferences("level", MODE_PRIVATE);
        final String current = result.getString("currentlevel", "0");

            button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                button3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {




                        Intent play2 = new Intent(MainActivity.this, Level2.class);
                        Intent play1 = new Intent(MainActivity.this, Level1.class);


                        if (current == "1")
                            startActivity(play2);

                        else if (current == "0")
                            startActivity(play1);

                    }
                });
            }});

Aucun commentaire:

Enregistrer un commentaire