从Mainactivity发送值并在另一个活动中显示该值

时间:2017-08-09 12:16:54

标签: java android

我正在尝试制作一个简单的测验应用,并且我想在另一个活动(另一个屏幕)中显示得分值,即当我按下提交按钮时,应该打开得分活动并显示总得分。

我尝试过使用意图,但它没有奏效。我是android编程的新手,可能会有一些愚蠢的错误。

这是MainActivity.java文件。

package com.example.android.conanquiz;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    int score = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    //Question 1 Methods
    public void question1_click(View view) {
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.haibara:
                if (checked) {
                    String correct = "Right Answer";
                    display_answer1(correct);
                    score++;
                }
                break;
            default: {
                String wrong = "Wrong Answer" + "\n" + "The right answer is " + getString(R.string.q1_o1);
                display_answer1(wrong);
            }
            break;
        }

    }

    private void display_answer1(String answer) {
        TextView quantityTextView = (TextView) findViewById(R.id.answer_1);
        quantityTextView.setText(answer);
    }

    //Question 2 Methods
    public void question2_click(View view) {
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.true_op:
                if (checked) {
                    String correct = "Right Answer";
                    display_answer2(correct);
                    score++;
                }
                break;
            default: {
                String wrong = "Wrong Answer" + "\n" + "The right answer is " + getString(R.string.q2_o1);
                display_answer2(wrong);
            }
            break;
        }

    }

    private void display_answer2(String answer) {
        TextView quantityTextView = (TextView) findViewById(R.id.answer_2);
        quantityTextView.setText(answer);
    }


    //Question 3 Methods
    public void question3_click(View view) {
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.q3_op3:
                if (checked) {
                    String correct = "Right Answer";
                    display_answer3(correct);
                    score++;
                }
                break;
            default: {
                String wrong = "Wrong Answer" + "\n" + "The right answer is " + getString(R.string.q3_o3);
                display_answer3(wrong);
            }
            break;
        }

    }

    private void display_answer3(String answer) {
        TextView quantityTextView = (TextView) findViewById(R.id.answer_3);
        quantityTextView.setText(answer);
    }


    //Question 4 Methods
    public void question4_click(View view) {
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.q4_op1:
                if (checked) {
                    String correct = "Right Answer";
                    display_answer4(correct);
                    score++;
                }
                break;
            default: {
                String wrong = "Wrong Answer" + "\n" + "The right answer is " + getString(R.string.q4_o1);
                display_answer4(wrong);
            }
            break;
        }

    }

    private void display_answer4(String answer) {
        TextView quantityTextView = (TextView) findViewById(R.id.answer_4);
        quantityTextView.setText(answer);
    }

    //Question 5 Methods

    public void question5_click(View view) {
        boolean checked = ((RadioButton) view).isChecked();

        // Check which radio button was clicked
        switch (view.getId()) {
            case R.id.q5_op3:
                if (checked) {
                    String correct = "Right Answer";
                    display_answer5(correct);
                    score++;
                }
                break;
            default: {
                String wrong = "Wrong Answer" + "\n" + "The right answer is " + getString(R.string.q5_o3);
                display_answer5(wrong);
            }
            break;
        }

    }

    private void display_answer5(String answer) {
        TextView quantityTextView = (TextView) findViewById(R.id.answer_5);
        quantityTextView.setText(answer);
    }



    //Submit Button
   public void onClickSubmit(View view){
        Intent scoreActivity = new Intent(MainActivity.this,Score.class);
        scoreActivity.putExtra("sendScore", score);
        startActivity(scoreActivity);


    }



}

这是另一个活动java(Score.java)文件

package com.example.android.conanquiz;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

public class Score extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);

        Intent scoreActivity = getIntent();
        int totalScore = scoreActivity.getIntExtra("sendScore", 0);
        displayScore(totalScore);

    }


    public void displayScore(int score) {
        TextView scoreTextView = (TextView) findViewById(R.id.score);
        scoreTextView.setText(score);
    }

}

1 个答案:

答案 0 :(得分:0)

  

尝试使用日志在按钮单击上打印score。如果得分不是空,而不是写在代码下面。

     

来自活动

Intent intent = new Intent(getBaseContext(), Score.class);
intent.putExtra("EXTRA_SCORE", score);
startActivity(intent);
  

致活动

 Intent intent = getIntent();
 int intValue = intent.getIntExtra("EXTRA_SCORE", 0);