I am trying to find out the syntax on how to make a for-loop to wait for a buttonclick before the loop proceeds to the next round. The app might seem meaningless, but the point is to find out this syntax.
In the code beneath buttonA is supposed to change number for each round. The text in buttonA should be the current number in the counter (x) of the for-loop. I want it to display "1" at first, and when one of the buttons are clicked I want it to display "2" (because the next round in the for-loop x should be "2"), and next time "3". Using the code beneath buttonA displays "3" as the app start, so it´s obvious that the for-loop has finished before the app starts.
Here is the code in the onCreate-method:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private TextView textView1;
private Button buttonA;
private Button buttonB;
private Button buttonC;
private TextView textView2;
private int a;
private int b;
private int c;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.txtView1);
buttonA = (Button) findViewById(R.id.btn1);
buttonB = (Button) findViewById(R.id.btn2);
buttonC = (Button) findViewById(R.id.btn3);
textView2 = (TextView) findViewById(R.id.txtView2);
final Random rndNumber = new Random();
for (int x = 1; x <= 3; x++){
buttonA.setText(Integer.toString(x));
buttonB.setText("5");
buttonC.setText("9");
buttonA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("Good, you pushed the right button");
}
});//End onClickListenerA
buttonB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("Wrong button");
}
});//End onClickListenerB
buttonC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("Wrong button ");
}
});//End onClickListenerA
}//End for-loop
}//End onCreate
Um. Don't.
You want a loop like that you certainly can't do it threaded like that. You need to set the variables in the activity, wait for the user interaction, then increment the variable you have set and call a function to set up the next round.
int x;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.txtView1);
buttonA = (Button) findViewById(R.id.btn1);
buttonB = (Button) findViewById(R.id.btn2);
buttonC = (Button) findViewById(R.id.btn3);
textView2 = (TextView) findViewById(R.id.txtView2);
final Random rndNumber = new Random();
x = 1;
setupRound();
}
private void setupRound() {
buttonA.setText(Integer.toString(x));
buttonB.setText("5");
buttonC.setText("9");
buttonA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("Good, you pushed the right button");
setupRound();
}
});//End onClickListenerA
buttonB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("Wrong button");
setupRound();
}
});//End onClickListenerB
buttonC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("Wrong button ");
setupRound();
}
});//End onClickListenerA
i++;
}