I know there are a few Android Timer posts, and I can't really see what I'm missing from them.
The user presses a button to begin the game, text is updated with the seconds that have passed, and, if the button is pressed after eight seconds, a congratulatory message is displayed.
public static class PlaceholderFragment extends Fragment {
public static long startTime;
public static int time;
public static boolean buttonClicked1;
public static boolean buttonClicked2;
public static boolean win;
public static TextView todd;
public static TextView tim;
public static Button bob;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
win = false;
bob = (Button)rootView.findViewById(R.id.button);
tim = (TextView)rootView.findViewById(R.id.message);
todd = (TextView)rootView.findViewById(R.id.timer);
startTime = System.currentTimeMillis();
time = 0;
buttonClicked1 = false;
buttonClicked2 = false;
bob.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
if(!buttonClicked1){
buttonClicked1 = true;
bob.setText("Push Here");
play();
}
else if(buttonClicked1) //Has game started
{
if(time>8)
buttonClicked2 = true;
}
}
});
return rootView;
}
public void play(){
playThread paul = new playThread();
paul.run();
}
public void winner(){
tim.setText("Congratulations");
}
public boolean checkWin(){
return buttonClicked2 ? true : false;
}
public void updateTime(){
if((System.currentTimeMillis() - startTime) % 1000 > 0){
startTime = System.currentTimeMillis();
time++;
}
todd.setText(""+time);
}
private class playThread implements Runnable{
public void run(){
System.out.println("Thread running");
while(!win){
updateTime();
win = checkWin();
winner();
}
}
}
}
}
I decided to implement Runnable instead of extending Thread for the playThread private class after reading this.
The game becomes unresponsive after hitting the start button, the text of which doesn't change to "Push Here".
I am, of course, a complete Android beginner, and I suspect the issue is with my implementation of a thread, but the problem may be elsewhere because I'm not too familiar with the programming style or conventions.
Thanks for looking this over!