如何只传递用户选择的字符串

当我单击一个按钮时,我需要该按钮仅传递选定的字符串。

如果 Integer 为 2,我会尝试传递一个特定的字符串。

显示这是我的第一个名为 PlayerChoose 的活动:

  //PI1 and PI2 is Strings from another Activity.

    PI1 = getIntent().getStringExtra("Player1Text");

    PI2 = getIntent().getStringExtra("Player2Text");


    //These bellow are buttons.

    Player1Btn = findViewById(R.id.PlayerToD1);

    Player2Btn = findViewById(R.id.PlayerToD2);


    //These Integers is to set 0 so the app is not broken.

    PlayerInt1 = 0;

    PlayerInt2 = 0;


    //Here im setting text to the buttons so you know what player you pick

    Player1Btn.setText(PI1);

    Player2Btn.setText(PI2);


    //These button adds 1 to the Integer PlayerInt1 so if you hit it 

    //twice it executes what is in the if statement.

    Player1Btn.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {

            PlayerInt1++;

            if (PlayerInt1 == 2) {

    Intent TruthOrDare1 = new Intent(PlayerChoose.this,TruthOrDare.class);

                TruthOrDare1.putExtras(getIntent());

                TruthOrDare1.putExtra("Player1Text", PI1);

                startActivity(TruthOrDare1);

            }

        }

    });

    //Same button as Player1Btn Does the same things.

    Player2Btn.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View view) {

            PlayerInt2++;

            if (PlayerInt2 == 2) {

    Intent TruthOrDare2 = new Intent(PlayerChoose.this,TruthOrDare.class);

                TruthOrDare2.putExtras(getIntent());

                TruthOrDare2.putExtra("Player2Text", PI2);

                startActivity(TruthOrDare2);

            }

        }

    });


    }

 }


我的问题是,只有当我在 PlayerChoose Activity 中点击 Player1Btn 和 PI2 如果我点击 Player2Btn 时,它才会传递两个字符串 PI1,PI2 而不是 PI1。如果我点击 Player1Btn 和 PI2 如果我点击 PI2,我怎么能只传递 PI1。


慕桂英3389331
浏览 124回答 2
2回答

紫衣仙女

看来你有不必要的字符串TruthOrDare1.putExtras(getIntent()); TruthOrDare2.putExtras(getIntent());在点击侦听器中,它同时添加了 PI1 和 PI2

慕妹3242003

因此,在名为 PlayerChoose 的第一个活动中很简单,您可以创建另一个字符串,无论我将其命名为 PIP1 的示例,这就是您必须做的:  //So Put PI1 as default string of PIP1.  PIP1 = PI1;  //Then in the Buttons OnClickListeners   Player1Btn.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View view) {            PlayerInt1++;            //If you click this button it will set PIP1 to the string you want to             //pass in this case PI1            PIP1 = PI1;            if (PlayerInt1 == 2) {          Intent TruthOrDare1 = new Intent(PlayerChoose.this, TruthOrDare.class);                TruthOrDare1.putExtras(getIntent());                //Intead of putting the text itself you put PIP1 thats changing                 //depens on what button you click                TruthOrDare1.putExtra("PlayerText", PIP1);                startActivity(TruthOrDare1);            }        }    });    Player2Btn.setOnClickListener(new View.OnClickListener() {        @Override        public void onClick(View view) {            PlayerInt2++;            //And here you Change the same string PIP1 to PI2            //Here you change the PIP1 to PI2 so PIP1 change to PI2 and not PI1            PIP1 = PI2;            if (PlayerInt2 == 2) {           Intent TruthOrDare2 = new Intent(PlayerChoose.this, TruthOrDare.class);                TruthOrDare2.putExtras(getIntent());                TruthOrDare2.putExtra("PlayerText", PIP1);                startActivity(TruthOrDare2);            }        }    });在名为 TruthOrDare 的第二个活动中,您可以更改此内容:    //So you only have 1 String Instead of 2 with 1 key     PI1 = getIntent().getStringExtra("PlayerText");    //One TextView    PlayerTurnName1 = findViewById(R.id.PlayerTurnText1);    PlayerTurnName1.setText(PI1); 这个概念是每次你按下按钮时 PIP1 都会改变,所以它不会传递所有的值。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java