Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android按钮文本颜色不变_Java_Android - Fatal编程技术网

Java Android按钮文本颜色不变

Java Android按钮文本颜色不变,java,android,Java,Android,我正在开发一个有问答选项的测验应用程序。单击错误答案(按钮)时,该特定按钮的文本颜色会变为红色,这是应该的。但是,当单击右按钮时,该特定按钮的文本颜色应变为绿色,等待一秒钟,然后转到下一个问题 我花了好几天的时间试图找出它为什么不起作用,在谷歌上搜索答案,但我仍然无法让它起作用 我尝试过使用处理程序和Runnable,但仍然不起作用 我已经发布了下面活动的完整代码 请帮忙 @Override protected void onCreate(Bundle savedInstanceState) {

我正在开发一个有问答选项的测验应用程序。单击错误答案(按钮)时,该特定按钮的文本颜色会变为红色,这是应该的。但是,当单击右按钮时,该特定按钮的文本颜色应变为绿色,等待一秒钟,然后转到下一个问题

我花了好几天的时间试图找出它为什么不起作用,在谷歌上搜索答案,但我仍然无法让它起作用

我尝试过使用处理程序和Runnable,但仍然不起作用

我已经发布了下面活动的完整代码

请帮忙

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

    Intent i = getIntent();
    subject = (Subject) i.getSerializableExtra(Globals.SUBJECT);
    questions = subject.getQuestions();
    subjectId = subject.getSubjectId();
    appPreferences = new AppPreferences();

    livesLeft = 3;
    index = 0;
    maxQuestions = questions.size();
    maxIndex = maxQuestions - 1;

    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    progressBar.setMax(maxQuestions);

    answer1 = (Button) findViewById(R.id.buttonAnswer1);
    answer2 = (Button) findViewById(R.id.buttonAnswer2);

    setQuestionsAndAnswers(index);

    TextView textView = (TextView) findViewById(R.id.textViewSubjectName);
    textView.setText(subject.getSubjectName());
}

private void setQuestionsAndAnswers(int index)
{
    currentQuestion = questions.get(index);

    // Set question
    TextView textViewQuestion = (TextView) findViewById(R.id.textViewQuestion);
    textViewQuestion.setText(currentQuestion.getQuestion());

    // Set correct answer
    correctAnswer = currentQuestion.getCorrectAnswer();
    sourceUrl = currentQuestion.getSourceUrl();
    sourceText = currentQuestion.getSourceText();

    // Set answer #1
    initializeButton(answer1, currentQuestion.getAnswer1());

    // Set answer #2
    initializeButton(answer2, currentQuestion.getAnswer2());
    // Set source
    TextView textViewSource = (TextView) findViewById(R.id.textViewSource);
    textViewSource.setText(sourceText);
    textViewSource.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);

    textViewSource.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(sourceUrl));
            startActivity(browserIntent);
        }
    });

    // Update progress
    updateProgress(index);
}

private void initializeButton(Button button, String answer)
{
    button.setText(answer);
    button.setTextColor(Color.WHITE);
    button.setBackgroundColor(Color.TRANSPARENT);
    button.setEnabled(true);

    button.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View view)
        {
            onClickContent(view);               
        }
    });
}

private void onClickContent(View view)
{
    Context context = getApplicationContext();
    final Button button = (Button) view;
    String answer = button.getText().toString();

    if (answer.equalsIgnoreCase(correctAnswer))
    {
        button.setTextColor(Color.GREEN);

        try
        {
            Thread.sleep(1000);
        } catch (InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        if (index == maxIndex)
        {
            appPreferences.editPreferences(context, Globals.PREFERENCE_KEY + subjectId, true);

            playSound(Globals.SOUND_QUIZ_COMPLETED);

            Toast toast = Toast.makeText(context, "Quiz \"" + subject.getSubjectName() + "\" finished",Toast.LENGTH_LONG);
            toast.show();
            finish();
        } else
        {
            playSound(Globals.SOUND_CORRECT_ANSER);

            // Go to next question
            index++;
            setQuestionsAndAnswers(index);
        }

    } else
    {
        if (livesLeft == 1)
        {
            playSound(Globals.SOUND_GAME_OVER);
            Toast toast = Toast.makeText(context, "Game over", Toast.LENGTH_LONG);
            toast.show();
            finish();
        } else
        {
            playSound(Globals.SOUND_WRONG_ANSWER);

            button.setTextColor(Color.RED);
            button.setEnabled(false);
            livesLeft--;
            TextView lives = (TextView) findViewById(R.id.textViewLives);
            lives.setText("Lives: " + livesLeft);
        }
    }
}

private void updateProgress(int progress)
{
    progressBar.setProgress(progress++);
}

private void playSound(int songId)
{
    MediaPlayer mp = MediaPlayer.create(getApplicationContext(), songId);
    mp.start();
}

这是我用于同样目的的东西。它应该有用

button.setTextColor(Color.GREEN);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
    public void run() {
        // Reset Color if you want to...
        // Start new question here
    }
}, 1000);

首先,
Thread.sleep(1000)在主线程上运行,导致主线程休眠,不建议这样做

您可以
查看.postDelayed
在延迟后加载下一个问题

private void onClickContent(View view)
{
    Context context = getApplicationContext();
    final Button button = (Button) view;
    String answer = button.getText().toString();

    if (answer.equalsIgnoreCase(correctAnswer))
    {
        button.setTextColor(Color.GREEN);

        if (index == maxIndex)
        {
            appPreferences.editPreferences(context, Globals.PREFERENCE_KEY + subjectId, true);

            playSound(Globals.SOUND_QUIZ_COMPLETED);

            Toast toast = Toast.makeText(context, "Quiz \"" + subject.getSubjectName() + "\" finished",Toast.LENGTH_LONG);
            toast.show();
            finish();
        } else
        {
            playSound(Globals.SOUND_CORRECT_ANSER);

            // Go to next question
            index++;
            // Go to next question after 1000ms
            view.postDelayed(new Runnable() {
                public void run() {
                    setQuestionsAndAnswers(index);
                }
            }, 1000); //here delays 1000ms
        }

    } 
    ...
}

我尝试了你的解决方案,但下一个问题在文本颜色变为绿色之前加载。run()应该包含移到下一个问题的代码。@ACetin如果你试图延迟加载下一个问题,该怎么办?@ACetin我更正了我的答案。Runnable中的所有内容都将在1000毫秒后启动,或者您希望的任何时间。karaokyo您是对的,下一个问题的加载应该在run()中,因为@yummi建议edgo重新使用处理程序,并且如果(answer.toLowerCase().equals(correctAnswer.toLowerCase()){
也使用此函数,而不是
if(answer.toLowerCase().equals(correct answer.toLowerCase()){
,看看它