首页 > 解决方案 > 在android中调用方法后如何恢复xml状态

问题描述

代码:

 private void updateQuestion() {
    mDatabaseReference.child("Users").child(RecieversId).child("Quiz").child("Question" + mQuestionNumber).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String question = dataSnapshot.child("Question").getValue().toString();
            answer = dataSnapshot.child("Answer").getValue().toString();
            option1 = dataSnapshot.child("Option1").getValue().toString();
            option2 = dataSnapshot.child("Option2").getValue().toString();
            option3 = dataSnapshot.child("Option3").getValue().toString();
            option4 = dataSnapshot.child("Option4").getValue().toString();
            que.setText(question);
            opt1.setText(option1);
            opt2.setText(option2);
            opt3.setText(option3);
            opt4.setText(option4);
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

    opt1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mQuestionNumber++;
            updateQuestion();
            qn.setText("Quesion : " + mQuestionNumber);
            if (option1.equals(answer)) {
                opt1.setBackgroundColor(Color.GREEN);
                mScore++;
                sco.setText("Score : " + mScore);
                updateQuestion();
            } else
                opt1.setBackgroundColor(Color.RED);
            sco.setText("Score : " + mScore);
            updateQuestion();
        }
    });
    opt2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mQuestionNumber++;
            updateQuestion();
            qn.setText("Quesion : " + mQuestionNumber);
            if (option2.equals(answer)) {
                opt2.setBackgroundColor(Color.GREEN);
                mScore++;
                sco.setText("Score : " + mScore);
                updateQuestion();
            } else
                opt2.setBackgroundColor(Color.RED);
            sco.setText("Score : " + mScore);
            updateQuestion();
        }
    });
    opt3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mQuestionNumber++;
            qn.setText("Quesion : " + mQuestionNumber);
            if (option3.equals(answer)) {
                opt3.setBackgroundColor(Color.GREEN);
                mScore++;
                sco.setText("Score : " + mScore);
                updateQuestion();
            } else
                opt3.setBackgroundColor(Color.RED);
            sco.setText("Score : " + mScore);
            updateQuestion();
        }
    });
    opt4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mQuestionNumber++;
            qn.setText("Quesion : " + mQuestionNumber);
            if (option4.equals(answer)) {
                opt4.setBackgroundColor(Color.GREEN);
                mScore++;
                sco.setText("Score : " + mScore);
                updateQuestion();
            } else
                opt4.setBackgroundColor(Color.RED);
            sco.setText("Score : " + mScore);
            updateQuestion();

        }
    });
    }
 }

只是想制作一个测验应用程序...在每次单击按钮后都会调用 updatequestion(在用户选择答案后)...如果正确,则按钮变为绿色,如果错误则变为红色...但即使在进入下一个按钮后该颜色仍然存在问题...我如何使其获得xml文件中提到的颜色...这是默认按钮颜色

标签: javaandroiddatabasefirebase

解决方案


在更新问题中,重置所有文本视图的颜色,因为您正在重用这些文本视图并且文本视图的状态将保持不变,因此请使用

mDatabaseReference.child("Users").child(RecieversId).child("Quiz").child("Question" + mQuestionNumber).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String question = dataSnapshot.child("Question").getValue().toString();
            answer = dataSnapshot.child("Answer").getValue().toString();
            option1 = dataSnapshot.child("Option1").getValue().toString();
            option2 = dataSnapshot.child("Option2").getValue().toString();
            option3 = dataSnapshot.child("Option3").getValue().toString();
            option4 = dataSnapshot.child("Option4").getValue().toString(); 
            //-------------------
            opt1.setBackgroundColor(Color.TRANSPARENT);
            opt2.setBackgroundColor(Color.TRANSPARENT);
            opt3.setBackgroundColor(Color.TRANSPARENT);
            opt4.setBackgroundColor(Color.TRANSPARENT);
            //-------------------
            que.setText(question);
            opt1.setText(option1);
            opt2.setText(option2);
            opt3.setText(option3);
            opt4.setText(option4);               
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

推荐阅读