gpt4 book ai didi

java - 如何仅传递用户选择的字符串

转载 作者:行者123 更新时间:2023-12-02 09:35:45 25 4
gpt4 key购买 nike

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

如果整数为 2,我尝试传递特定字符串。

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

    //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);
}
}
});

}
}

这是我的第二个 Activity ,名为“TruthOrDare”:

    //PI1 Should take what i passed with "Player1Text" and save it as a 
//String
//PI2 Should take what i passed with "Player2Text" and save it as a
//String
PI1 = getIntent().getStringExtra("Player1Text");
PI2 = getIntent().getStringExtra("Player2Text");


//PlayerTurnName1 and PlayerTurnName2 is textviews
PlayerTurnName1 = findViewById(R.id.PlayerTurnText1);
PlayerTurnName2 = findViewById(R.id.PlayerTurnText2);

//Here i set text to textview whatever have been passes PI1 or PI2
//Depending
PlayerTurnName1.setText(PI1);
PlayerTurnName2.setText(PI2);

我的问题是,仅当我在 PlayerChoose Activity 中点击 Player1Btn 时,它才传递字符串 PI1、PI2 而不是 PI1;如果我点击 Player2Btn,则传递 PI2。如果我点击 Player1Btn,则如何仅传递 PI1;如果点击 PI2,则如何仅传递 PI2。

最佳答案

您似乎有不必要的字符串

TruthOrDare1.putExtras(getIntent());
TruthOrDare2.putExtras(getIntent());

在点击监听器中,添加 PI1 和 PI2

关于java - 如何仅传递用户选择的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57526595/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com