Java 按钮应该在字符串数组中单击5次,停止5秒,然后继续(单击5次),错误
我的问题是我无法让我的按钮在文本视图中显示的字符串数组中连续单击5次, 每当maxclicks(5)和currentnumber达到5时,它就会停止工作,我一直在尝试创建if条件来解决它,不知何故,我必须操纵我的currentnumber不为5,因为如果maxclicks==currentnumber,我的按钮被启用 在下面的代码中,它在第一次单击5次后停止 下面是代码:Java 按钮应该在字符串数组中单击5次,停止5秒,然后继续(单击5次),错误,java,android,arrays,android-button,Java,Android,Arrays,Android Button,我的问题是我无法让我的按钮在文本视图中显示的字符串数组中连续单击5次, 每当maxclicks(5)和currentnumber达到5时,它就会停止工作,我一直在尝试创建if条件来解决它,不知何故,我必须操纵我的currentnumber不为5,因为如果maxclicks==currentnumber,我的按钮被启用 在下面的代码中,它在第一次单击5次后停止 下面是代码: public class MainActivity extends AppCompatActivity { int cu
public class MainActivity extends AppCompatActivity {
int currentnumber = 0;
int mod = 5;
TextView display = findViewById(R.id.tx);
Handler handler = new Handler();
int delay = 5000;
int maxclicks = list.length;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
final String[] list = res.getStringArray(R.array.xyz);
final Button next_button = findViewById(R.id.next_btn);
{
((TextView) findViewById(R.id.tx)).setText(list[currentnumber]);
next_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(currentnumber == maxclicks){
currentnumber = 0;
}
if (currentnumber % mod == 0) {
next_button.setEnabled(false);
handler.postDelayed(new Runnable() {
@Override
public void run() {
//the button will unlock after the delay specified
next_button.setEnabled(true);
currentnumber++;
}
}, delay);
}
else {
display.setText(list[currentnumber]);
currentnumber++;
}
}
});
}
}
}
欢迎来到SO:)我尽了最大努力理解您的解释,所以这是我解决您问题的方法,不要忘记您可以将您的VAR设置为全局,以避免最终和一个元素数组:
public class MainActivity extends AppCompatActivity {
private int currentnumber,mod,delay,Curclicks;
private TextView display;
private Handler handler;
private Button next_button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setting);
//Binding
display = findViewById(R.id.tx);
next_button = findViewById(R.id.next_button);
//getResources
Resources res = getResources();
//getting the data ready
String[] list = {"1","2","3","4","5","6","7"};
//assign vars
handler = new Handler();
currentnumber = 0;
Curclicks=0;
mod = 5;
delay = 5000;
//initial view
next_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(currentnumber == list.length){
currentnumber = 0;
}
if (Curclicks == mod-1) {
next_button.setEnabled(false);
display.setText(list[currentnumber]);
currentnumber++;
handler.postDelayed(new Runnable() {
@Override
public void run() {
//the button will unlock after the delay specified
next_button.setEnabled(true);
Curclicks = 0;
}
}, delay);
}
else {
display.setText(list[currentnumber]);
currentnumber++;
}
Curclicks++;
}
});
}
}
欢迎来到SO。我们需要知道“错误”对你来说意味着什么,在这个特定的案例中,你到底想做什么还不清楚。将maxclicks设置为4没有多大作用,因为下一行会将其设置为5。因此,我的错误意思是,如果我在第6个字符串处开始单击,则我的按钮不会单击5次(在我设置按钮超时后)我真的不知道为什么,但我认为这是因为数组中的数字和maxclicks是一样的对不起,我不善于解释我基本上想要的是我的按钮在字符串数组中每次都单击5次,直到它超时并再次启动(我已经这样做了)。在你的代码片段中,currentNumber将始终保持为零。另一方面,maxclicks永远不会为零:您不能将其从5更改为4,因为5!=0,因此实际上,maxclicks始终为5。也许你发布的代码中有一两个输入错误?我想你在onClick()方法中寻找类似“currentNumber++”的东西。我做了很多假设,但我猜你是在尝试允许5次点击,然后禁用按钮?