我试图找出关于如何在循环进入下一轮之前使for循环等待按钮的语法。该应用程序似乎毫无意义,但重点是找出这种语法。
在按钮A下方的代码中,应该更改每轮的数字。 buttonA中的文本应该是for循环的计数器(x)中的当前数字。我希望它首先显示“1”,当点击其中一个按钮时,我希望它显示“2”(因为for循环x中的下一轮应为“2”),下次“3” ”。使用buttonA下方的代码显示“3”作为应用程序启动,因此很明显for-loop已在应用程序启动之前完成。
以下是onCreate方法中的代码:
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
答案 0 :(得分:1)
我不认为你的情况需要一个for循环。
尝试使用计数器:
private int mCounterX;
@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();
mCounterX= 1;
buttonA.setText(Integer.toString(mCounterX));
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");
mCounterX++;
buttonA.setText(Integer.toString(mCounterX));
}
});//End onClickListenerA
buttonB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("Wrong button");
mCounterX++;
buttonA.setText(Integer.toString(mCounterX));
}
});//End onClickListenerB
buttonC.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
textView2.setText("Wrong button ");
mCounterX++;
buttonA.setText(Integer.toString(mCounterX));
}
});//End onClickListenerA
}//End onCreate
答案 1 :(得分:1)
庵。唐&#39;吨
你想要一个像你一样可以做到这样的循环的循环。您需要在活动中设置变量,等待用户交互,然后递增您设置的变量并调用函数来设置下一轮。
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++;
}