“Else if”语句不做任何事情(制作android测试)

时间:2012-10-02 22:39:29

标签: java android eclipse if-statement statements

我正在制作我的第一个Android应用程序并且我想制作一个测试应用程序,我正在使用变量和“If”/“Else if”语句以便更改问题的文本(TextView)和答案(RadioButtons)。问题是,当用户第一次按下按钮时,答案和问题会改变,但当用户第二次按下按钮时,问题和答案不会改变,我不知道为什么......

这是我的活动:

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 个答案:

答案 0 :(得分:3)

要在每次点击活动中为您的变量添加1,请尝试使用此代码:

pregunta++;

(了解这与pregunta += 1;pregunta = pregunta + 1;

相同

你写的pregunta = + 1;意味着pregunta = +1;只不过是

pregunta = 1;