正确答案的进度条

时间:2016-12-02 18:05:59

标签: java android android-studio progress-bar

我正在Android工作室做一个小数学游戏,从第一级开始,玩家需要连续回答10个问题。我有得分计数,当玩家获得一定分数时,他们会达到另一个等级。我想添加一个进度条,以正确的答案进行。所以说当玩家需要连续10个正确答案但有5个时,它将被填满50%等。 这是计算得分的代码:

  Button answerButton = (Button) findViewById(R.id.answerButton);
    answerButton.setOnClickListener(new View.OnClickListener(){
        @Override

        public void onClick(View v) {
            try{
                answerUser = Integer.parseInt(numberEdit.getText().toString());
            }
            catch (NumberFormatException e)
            {
                answerUser = 0;
                Toast.makeText(context,
                        "Error, masdaohfisahfi9aw",
                        Toast.LENGTH_SHORT).show();
            }
            if(answer == answerUser)
            {
                TextView Scores = (TextView) findViewById(R.id.scoreText);
                score ++;

                String String3 = String.valueOf(score);
                Scores.setText(String3);
                answerText.setText("Well done!");
                do {
                    Random rand = new Random();
                    Random rand1 = new Random();
                    number = rand.nextInt(10) + 1;
                    numbers1 = rand1.nextInt(10) + 1;
                    TextView number1 = (TextView) findViewById(R.id.number1);
                    TextView number2 = (TextView) findViewById(R.id.number2);

                    String String1 = String.valueOf(number);
                    String String2 = String.valueOf(numbers1);
                    number1.setText(String1);
                    number2.setText(String2);
                    answer = number + numbers1;
                    answerUser = 0;
                    numberEdit.setText("");

                }

                while(answer==answerUser);
                if(score == 10)
                {
                    alert();
                }
            }
            else
            {
                answerText.setText("Wrong");
                TextView Scores = (TextView) findViewById(R.id.scoreText);
                score = 0;
                String String3 = String.valueOf(score);
                Scores.setText(String3);
            }
        }

    });

1 个答案:

答案 0 :(得分:1)

在你班上:

ProgressBar progressBar

在你的onCreate:

 progressBar = (ProgressBar) findViewById(R.id.answerProgress);
 progressBar.setMax(10);
 progressBar.setProgress(0);

然后在代码的这一部分

 if(answer == answerUser)
            {
                TextView Scores = (TextView) findViewById(R.id.scoreText);
                score ++;
                progressBar.setProgress(score);
                ....
            } 

在XML中,指定您需要一个水平进度条,否则它将是循环的:

<ProgressBar
    android:layout_width="match_parent"
    android:layout_height="50dp"
    style="@android:style/Widget.Holo.ProgressBar.Horizontal"
    android:id="@+id/answerProgress"
    />