作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在为三名玩家的本地纸牌游戏制作一个应用程序,其中有两项 Activity 。一是计算结果的计算器,二是显示上述结果。然而,由于我不是java和一般编程方面的专家,所以它不起作用。
现在我的应用程序中发生的情况是,玩家一号(在我的例子中是 Tom)的结果是从计算器 Activity 中获取的,并显示在第一个 textView
和玩家一号的姓名中。但是,当我重复此操作, Intent 在第二个 textView
中显示第二个结果时,它只会覆盖第一个 textView
。我已经尝试过以下操作:
startActivity
startActivityForResult
finish()
完成计算器 Activity (称为 MainActivity)Bundle
而不是Intent
,但是但这些都不起作用。在计算器 Activity (称为 MainActivity)中:
final Button zapsat = (Button)findViewById(R.id.button3);
zapsat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final TextView tomVysledek = (TextView)findViewById(R.id.textView_tom2);
//final TextView tataVysledek = (TextView)findViewById(R.id.textView_tom3);
//final TextView kubaVysledek = (TextView)findViewById(R.id.textView_tom4);
Intent intent = new Intent(MainActivity.this, Results.class);
intent.putExtra("tomVysledek", tomVysledek.getText().toString());
startActivity(intent);
}
});
在 Results Activity 的 onCreate
中(我想在其中显示结果):
TextView textView_tomV1 = (TextView) findViewById(R.id.textView_tomV1);
TextView textView_tomV2 = (TextView) findViewById(R.id.textView_tomV2);
.
.
.
TextView textView_tomV5 = (TextView) findViewById(R.id.textView_tomV5);
int tomZapis = 0;
Intent intent = getIntent();
try {
tomZapis = Integer.parseInt(intent.getStringExtra("tomVysledek"));
} catch (Exception e){}
String tomZapis_tv = Integer.toString(tomZapis);
if (textView_tomV1.getText().equals("0")) {
textView_tomV1.setText(tomZapis_tv);
} else if (textView_tomV2.getText().equals("0")){
textView_tomV2.setText(tomZapis_tv);
} else if (textView_tomV3.getText().equals("0")){
textView_tomV3.setText(tomZapis_tv);
} else if (textView_tomV4.getText().equals("0")) {
textView_tomV4.setText(tomZapis_tv);
} else if (textView_tomV5.getText().equals("0")) {
textView_tomV5.setText(tomZapis_tv);
} else {
Toast.makeText(Results.this, "Už není místo :(", Toast.LENGTH_LONG).show();
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intentres = new Intent(Results.this, MainActivity.class);
startActivityForResult(intentres, 1);
}
});
正如我所说,它总是在第一个 textView
(textView_tomV1
) 中显示结果,而不是找到第一个空的 textView
并填充它与变量。我相信这是因为 startActivity
实际上重新启动了 Activity,但我不确定这一点,即使是这样,我也不知道该如何处理它。
最佳答案
我不确定它是否能解决您的问题,但是当您写下此内容时
else if (textView_tomV2.getText().equals("0"))
您将“Editable”与字符串进行比较...你应该像这样添加 toString()
textView_tomV2.getText().toString().equals("0")
它会比较字符串
关于java - 如何制作一个记分板,在另一项 Activity 中计算结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56414589/
我是一名优秀的程序员,十分优秀!