正如主题所说,我正在寻找一种方法,如果首先单击该按钮,则首先运行,如果单击该按钮,则运行下一个,这是我尝试过的
final Button button = (Button) findViewById(R.id.welcomeButton);
final TextView textView = (TextView) findViewById(R.id.welcomeText);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View p1)
{
int x = 1;
if (x == 1)
{
textView.setText(R.string.step1);
button.setText("Next");
// heres increment of the variable x so on next click x will be 2 and the next if will be run instead
x++;
}
if (x == 2)
{
textView.setText(R.string.step2);
button.setText("Next");
}
}
});
如您所见,第一个增加,if
因此它增加了变量x,下次单击按钮x时,x是2,第二个将运行,但是问题是在第一个按钮上单击时,它先运行,if
然后增加变量,然后再运行if
我希望它仅在第一次单击时才能首先运行。我怎样才能用这种方法做到这一点?是否最好使用case
if,如果是的话,请给我举个例子,谢谢
您需要使x
您的类的成员变量。因此,您必须在课程中的某处定义它,例如
private int x = 1;
并将您的onClick
方法更改为此
@Override
public void onClick(View p1)
{
if (x == 1)
{
textView.setText(R.string.step1);
button.setText("Next");
// heres increment of the variable x so on next click x will be 2 and the next if will be run instead
x++;
}
if (x == 2)
{
textView.setText(R.string.step2);
button.setText("Next");
}
}
您可能也需要在x
某处重设
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句