我正在制作我的第一个 Android 应用程序,我想制作一个测试应用程序,我使用变量和“If”/“Else if”语句来更改问题的文本(TextView)和答案(单选按钮)。问题是,当用户第一次按下按钮时,答案和问题会发生变化,但是当用户第二次按下按钮时,问题和答案不会改变,我不知道为什么......
这是我的 Activity :
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class Test extends Activity {
TextView titulo;
RadioGroup buttons;
RadioButton opcn1;
RadioButton opcn2;
RadioButton opcn3;
RadioButton opcn4;
int pregunta = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memetest);
titulo = (TextView) findViewById(R.id.textView1);
buttons = (RadioGroup) findViewById(R.id.radioGroup1);
opcn1 = (RadioButton) findViewById(R.id.radio0);
opcn2 = (RadioButton) findViewById(R.id.radio1);
opcn3 = (RadioButton) findViewById(R.id.radio2);
opcn4 = (RadioButton) findViewById(R.id.radio3);
Button siguiente = (Button) findViewById(R.id.siguiente);
siguiente.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pregunta = + 1;
if (pregunta == 1){
titulo.setText(getString(R.string.Q2));
opcn1.setText(getString(R.string.Q2O1));
opcn2.setText(getString(R.string.Q2O2));
opcn3.setText(getString(R.string.Q2O3));
opcn4.setText(getString(R.string.Q2O4));
}
else if (pregunta == 2){
titulo.setText(getString(R.string.Q3));
opcn1.setText(getString(R.string.Q3O1));
opcn2.setText(getString(R.string.Q3O2));
opcn3.setText(getString(R.string.Q3O3));
opcn4.setText(getString(R.string.Q3O4));
}
};
});
}
}
我尝试使用“break”,但问题仍然存在。
请帮忙。
最佳答案
要在每个点击事件上将 1
添加到变量,请尝试使用以下代码:
pregunta++;
(理解这与 pregunta += 1;
或 pregunta = pregunta + 1;
相同)
你写的 pregunta = + 1;
意味着 pregunta = +1;
只不过是
pregunta = 1;
关于java - "Else if"语句不执行任何操作(进行 Android 测试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12699453/