用户输入更改按钮将打开的活动

时间:2015-05-27 22:55:47

标签: android button android-activity

我尝试制作一个简单的加法游戏,其中包含两个随机生成的数字。当用户输入答案时,在按下按钮后,它将根据他们的答案进行正确或不正确的活动。我无法获得我的" additionAnswer"进行课程并将其与用户输入的数字进行比较。这是代码:

public class Addition extends ActionBarActivity {
    Button button9;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addition);
        addListenerOnButton();
        Random addition1 = new Random();
        int additionInt1 = addition1.nextInt(100)+1;
        TextView additionText1 = (TextView) findViewById(R.id.textView22);
        String additionString1 = String.valueOf(additionInt1);
        additionText1.setText(additionString1);
        Random addition2 = new Random();
        int additionInt2 = addition2.nextInt(100)+1;
        TextView additionText2 = (TextView) findViewById(R.id.textView21);
        String additionString2 = String.valueOf(additionInt2);
        additionText2.setText(additionString2);
        int additionAnswer = additionInt1 + additionInt2;
    }

    public void addListenerOnButton() {
        final Context context = this;
        button9 = (Button) findViewById(R.id.button9);
        button9.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if(textview5 = additionAnswer) {
                    Intent intent = new Intent(context, Correct.class);
                    startActivity(intent);
                }
                else{
                    Intent intent = new Intent(context, Incorrect.class);
                    startActivity(intent);
                }

            }
        });
    }

1 个答案:

答案 0 :(得分:0)

您需要将text转换为integer

public void addListenerOnButton() {
    final Context context = this;
    button9 = (Button) findViewById(R.id.button9);
    button9.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // check for 'textview5.getText()' is null
            if (textview5.getText()!=null && (Integer.valueOf(textview5.getText().toString()) == additionAnswer)) {
                Intent intent = new Intent(context, Correct.class);
                startActivity(intent);
            } else {
                Intent intent = new Intent(context, Incorrect.class);
                startActivity(intent);
            }

        }
    });
}