作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我单击按钮时,我需要该按钮仅传递选定的字符串。
如果整数为 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/
我是一名优秀的程序员,十分优秀!