【问题标题】:How to restore xml state after a method is called in android在android中调用方法后如何恢复xml状态
【发布时间】:2018-05-21 08:20:21
【问题描述】:

代码:

 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文件中提到的颜色...这是默认按钮颜色

【问题讨论】:

    标签: java android database firebase


    【解决方案1】:

    最简单的方法是将其添加到您的 onDataChanged() 方法中:

    opt1.setBackgroundColor(<default_color_reference>);
    opt2.setBackgroundColor(<default_color_reference>);
    opt3.setBackgroundColor(<default_color_reference>);
    opt4.setBackgroundColor(<default_color_reference>);
    

    在您设置文本之后。并确保 &lt;default_color_reference&gt; 匹配您想要的原始背景颜色,假设您在 XML 文件中专门设置了它。

    【讨论】:

    • 对不起,我格式错了。我的意思是“Color.DEFAULT”应该与设置为 XML 引用的按钮的颜色资源文件匹配。不完全复制和粘贴我的答案。如果颜色不是已经设置的特定颜色,“Color.TRANSPARENT”也可以工作。 (例如,可能 XML 背景设置为一组特定的灰色或淡蓝色或其他东西)
    【解决方案2】:

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

    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) {
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      相关资源
      最近更新 更多