我在使用 TextView 中的字符串的 OnRestoreInstanceState 时遇到问题。这个字符串基本上是一个箭头 (<<),它指向游戏中轮到谁了。
基本上这是我下面的代码:
private TextView textViewPlayerOneTurn;
...
private void switchPlayerTurn(){
if (playerOneMove){
textViewPlayerOneTurn.setVisibility(View.VISIBLE);
textViewPlayerTwoTurn.setVisibility(View.INVISIBLE);
}
else{
textViewPlayerOneTurn.setVisibility(View.INVISIBLE);
textViewPlayerTwoTurn.setVisibility(View.VISIBLE);
}
...
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("textViewPlayerOneTurn", textViewPlayerOneTurn.toString());
outState.putString("textViewPlayerTwoTurn", textViewPlayerTwoTurn.toString());
outState.putBoolean("playerOneMove", playerOneMove);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
playerOneMove = savedInstanceState.getBoolean("playerOneMove");
}
我的问题是,当我旋转屏幕时,指针始终默认指向玩家的回合。我可能找错了树,但我的想法是 onRestoreInstanceState,所以我想在该方法中包含以下内容:
textViewPlayerOneTurn = savedInstanceState.getString("textViewPlayerOneTurn"); textViewPlayerTwoTurn = savedInstanceState.getString("textViewPlayerTwoTurn");
但它给我错误说明它需要 android.widget.TextView 并且它找到了 Java.lang.String
我该如何解决这个问题?
最佳答案
textViewPlayerOneTurn.toString()
返回在 TextView 类的 toString()
方法中定义的 TextView 对象的字符串表示形式。
textViewPlayerOneTurn.getText().toString()
将返回显示在 TextView
上的预期字符串。
代替 textViewPlayerOneTurn.toString()
使用 textViewPlayerOneTurn.getText().toString()
关于java - 无法将 getString 用于 textView - android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50674085/