如何将多个生成的值作为输入传递给另一个活动

时间:2016-09-15 05:46:21

标签: java android random

我正在开发一个数学测验应用程序。它由9个级别组成,每个级别包含10个问题,这些问题从数组生成随机操作数。在特定级别,它生成随机操作数,我必须将这些操作数传递到另一个级别。

例如:如果第1级第1个输入是随机的4和3,则第2级第1个输入应该是3和4,同样它应该将所有10个设置操作数传递到另一个级别。我有共享代码。

public class levelFour extends AppCompatActivity implements View.OnClickListener {

    int qid = 0,score=0;
    private int answer=0,operand1=0,operand2=0;
    private int[] level4op1={0,1,2,3,4,5,6,7,8,9};
    private int[] level4op2={0,1,2,3,4,5,10};
    private Random random;

    private int enteredInput = 0;

    //Declare ui elements
    private TextView question, answerTxt, responseTxt, timeTxt;
    private ImageView response;
    private Button btn_one, btn_two, btn_three, btn_four, btn_five, btn_six, btn_seven, btn_eight, btn_nine, btn_zero,
            btn_clr;

    private long startTime = 0L;
    private Handler customHandler = new Handler();
    long timeInMilliseconds = 0L;
    long timeSwapBuff = 0L;
    long updatedTime = 0L;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainpage);
        Log.e("tag","correct : " );

        // Get input from Text View
        question = (TextView)findViewById(R.id.question);
        answerTxt = (TextView)findViewById(R.id.answer);
        response = (ImageView)findViewById(R.id.response);
        responseTxt = (TextView)findViewById(R.id.responseTxt);
        timeTxt=(TextView) findViewById(R.id.timeTxt);

        // to hide the response
        response.setVisibility(View.INVISIBLE);

        //Get input from Buttons

        btn_one = (Button)findViewById(R.id.btn_one);
        btn_two = (Button)findViewById(R.id.btn_two);
        btn_three = (Button)findViewById(R.id.btn_three);
        btn_four = (Button)findViewById(R.id.btn_four);
        btn_five = (Button)findViewById(R.id.btn_five);
        btn_six = (Button)findViewById(R.id.btn_six);
        btn_seven = (Button)findViewById(R.id.btn_seven);
        btn_eight = (Button)findViewById(R.id.btn_eight);
        btn_nine = (Button)findViewById(R.id.btn_nine);
        btn_zero = (Button)findViewById(R.id.btn_zero);
        btn_clr = (Button)findViewById(R.id.btn_clr);

        //onclick functions

        btn_one.setOnClickListener(this);
        btn_two.setOnClickListener(this);
        btn_three.setOnClickListener(this);
        btn_four.setOnClickListener(this);
        btn_five.setOnClickListener(this);
        btn_six.setOnClickListener(this);
        btn_seven.setOnClickListener(this);
        btn_eight.setOnClickListener(this);
        btn_nine.setOnClickListener(this);
        btn_zero.setOnClickListener(this);
        btn_clr.setOnClickListener(this);

        random= new Random();
        //To randomly generate questions
        selectQuestion();
        startTime = SystemClock.uptimeMillis();
        customHandler.postDelayed(updateTimerThread, 0);
    }

    private void selectQuestion() {
        enteredInput = 0;
        answerTxt.setText("?");
        //get random operand
        operand1=getOperand1();
        operand2=getOperand2();
        //show question
        question.setText(operand1 + " * " + operand2);
        answer=operand1*operand2;
        qid++;
        Log.e("tag","qid : " + qid);
    }

    private int getOperand1() {
        operand1=random.nextInt(level4op1.length);
        return operand1;


    }
    private int getOperand2() {
        operand2=random.nextInt(level4op2.length);
        return operand2;
    }

    private Runnable updateTimerThread = new Runnable() {

        public void run() {

            timeInMilliseconds = SystemClock.uptimeMillis() - startTime;

            updatedTime = timeSwapBuff + timeInMilliseconds;

            int secs = (int) (updatedTime / 1000);
            int mins = secs / 60;
            secs = secs % 60;
            int milliseconds = (int) (updatedTime % 1000);
            timeTxt.setText("" + mins + ":"
                    + String.format("%02d", secs));
            customHandler.postDelayed(this, 0);
        }

    };

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.btn_one || view.getId() == R.id.btn_two
                || view.getId() == R.id.btn_three || view.getId() == R.id.btn_four
                || view.getId() == R.id.btn_five || view.getId() == R.id.btn_six
                || view.getId() == R.id.btn_seven || view.getId() == R.id.btn_eight
                || view.getId() == R.id.btn_nine || view.getId() == R.id.btn_zero) {

            //final MediaPlayer correctMP=MediaPlayer.create(this,R.raw.correct);
            //final MediaPlayer wrongMP=MediaPlayer.create(this,R.raw.wrong);

            Spannable word = new SpannableString("Correct");
            word.setSpan(new ForegroundColorSpan(Color.GREEN), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            Spannable wordTwo = new SpannableString("Wrong");
            wordTwo.setSpan(new ForegroundColorSpan(Color.RED), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

            if (answer <= 9) {
                Log.e("tag","ans : " + answer);
                //get number from tag
                int enteredNum = Integer.parseInt(view.getTag().toString());
                if (answerTxt.getText().toString().endsWith("?"))
                    answerTxt.setText("" + enteredNum);
                if (enteredNum == answer) {
                    //      correctMP.start();
                    score++;
                    Log.e("tag","score : " + score);
                    response.setImageResource(R.drawable.tick);
                    response.setVisibility(View.VISIBLE);
                    responseTxt.setText(word);
                } else {
                    //    wrongMP.start();
                    Log.e("tag","score : " + score);
                    response.setImageResource(R.drawable.cross);
                    response.setVisibility(View.VISIBLE);
                    responseTxt.setText(wordTwo);
                }
                if(qid < 10){
                    selectQuestion();
                }
                else {
                    Intent intent=new Intent(levelFour.this,last.class);
                    Bundle b= new Bundle();
                    b.putInt("score",score);
                    intent.putExtras(b);
                    startActivity(intent);
                }
            }
            else if (answer > 9) {
                Log.e("tag","ans : " + answer);
                int enteredNum = Integer.parseInt(view.getTag().toString());
                enteredInput = enteredInput * 10 + enteredNum;
                answerTxt.setText("" + enteredInput);
                Log.e("tag","enteredInput : " + enteredInput);
                if (enteredInput == answer) {
                    Log.e("tag","correct : " );
                    //  correctMP.start();
                    score++;
                    Log.e("tag","score : " + score);
                    response.setImageResource(R.drawable.tick);
                    response.setVisibility(View.VISIBLE);
                    responseTxt.setText(word);
                    if(qid < 10){
                        selectQuestion();
                    }
                    else {
                        Intent intent=new Intent(levelFour.this,last.class);
                        Bundle b= new Bundle();
                        b.putInt("score",score);
                        intent.putExtras(b);
                        startActivity(intent);
                    }
                }
                else if (enteredInput > 9) {
                    Log.e("tag","wrong : " );
                    //wrongMP.start();
                    Log.e("tag","score : " + score);
                    response.setImageResource(R.drawable.cross);
                    response.setVisibility(View.VISIBLE);
                    responseTxt.setText(wordTwo);
                    if(qid < 10){
                        selectQuestion();
                    }
                    else {
                        Intent intent=new Intent(levelFour.this,last.class);
                        Bundle b= new Bundle();
                        b.putInt("score",score);
                        intent.putExtras(b);
                        startActivity(intent);
                    }
                }
            }
        }
        else if (view.getId() == R.id.btn_clr)
        {
            answerTxt.setText("?");
            enteredInput=0;
        }
    }
 }

这是为10个问题生成随机值的一个级别,我想使用在另一个级别中随机生成的相同问题顺序。 例如:

    Level 1 input 1: 4 and 3 input 2: 2 and 3 (generates randomly)
    Level 2 input 1: 3 and 4 input 2: 3 and 2 (from level 1 input)

1 个答案:

答案 0 :(得分:0)

您应该使用一个viewpager来控制滑动并将数据保存到活动级别。比10个不同的活动更容易。