【问题标题】:Progress bar on correct answers正确答案的进度条
【发布时间】:2016-12-02 18:05:59
【问题描述】:

我正在 android studio 中做一个小数学游戏,从第一级开始,玩家需要连续回答 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);
            }
        }

    });

【问题讨论】:

  • 您的问题到底是什么?如何添加进度条?如何更新它?或者使用哪个视图(例如,您也可以使用步进器)?
  • 我知道如何添加它,只是如何根据连续正确答案的数量来更新它。我必须使用进度条

标签: java android android-studio progress-bar


【解决方案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"
    />

【讨论】:

  • 我很确定这应该可以工作,但我得到一个运行时异常“尝试在空对象引用上调用虚拟方法 'void android.widget.ProgressBar.setMax(int)'”。我有设计、xml 和课堂调用的栏,我不知道我应该去哪里看?
  • 您是否在 XML 中指定它是水平的?查看我的编辑。
  • 好的,我修复了它,而不是将代码放在 onCreate 中,而是将它放在 if(answer == answerUser) 中。感谢您的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-13
  • 2018-11-30
  • 2018-06-28
  • 1970-01-01
  • 2016-08-17
  • 1970-01-01
  • 2020-10-10
相关资源
最近更新 更多