gpt4 book ai didi

java - 我的算法始终无法满足启动 Activity 的要求?

转载 作者:行者123 更新时间:2023-12-01 17:41:45 25 4
gpt4 key购买 nike

我创建了一款 Android 游戏,但它无法正常运行。

主要思想

用户单击 CheckedTextView 三次,第三次单击后,将启动第二个 Activity

问题

第二个 Activity 未开始。

ChechedTextView in my app ]

带有算法的代码:

public class StartOfTheGame extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.startofthegame);
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
final CheckedTextView checkedTextView = findViewById(R.id.checked_textview);
checkedTextView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String[] seasons = {"1", "2", "3", "4"};
int n = (int)Math.floor(Math.random() * seasons.length);
checkedTextView.toggle();
int a = 0;
if(checkedTextView.isChecked()) {
checkedTextView.setChecked(false);
checkedTextView.setText(seasons[n]);
a++;
}
if (a == 3){
try {
Intent intent = new Intent(StartOfTheGame.this, SecondStageOfTheGame.class);
startActivity(intent);
finish();
} catch (Exception e) {

}
}
}

});

最佳答案

a 是局部变量。因此,每次单击时,a 首先会在 int a = 0;

处设置为 0

尝试下面的代码:

checkedTextView.setOnClickListener(new View.OnClickListener(){

private int a = 0; // Add this here

@Override
public void onClick(View v) {
...
// int a = 0; // Remove this
...
if (a == 3){
try {
Intent intent = new Intent(StartOfTheGame.this, SecondStageOfTheGame.class);
startActivity(intent);
finish();
} catch (Exception e) {
}
}
}
}

关于java - 我的算法始终无法满足启动 Activity 的要求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60192412/

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