Issue
My problem is I cant get my button to consistently click 5 times through a string array which is displayed in a TextView , whenever the maxclicks(5) and currentnumber get to 5 it stops working , Ive been trying to create if conditions to work around it, well somehow I have to manipulate my currentnumber to NOT be 5 because IF maxclicks == currentnumber my button is enabled.
In the Code below it stops just afte the first time of clicking 5 times.
so here is the Code :
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++;
}
}
});
}
}
}
Solution
welcome to SO :) I did my best to understand on your explanation,so this is my solution for your problem and don't forget you can make your vars global to avoid final and one element array thing:
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++;
}
});
}
}
Answered By - Badran
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.