在“多项选择”测验应用中将输入类型从复选框更改为editext(针对某些问题)

我有一个android测验应用程序源代码。复选框类型位于其中。但是对于某些问题,我需要做为edittext的答案。如何将输入类型从复选框转换为edittext。我将附上解释要求的图像。 我在下面提供GitHub链接

测验应用程序的源代码:

https://github.com/tendentious/Multiple-Choice-Questions-Android-App

XML文件:

https://github.com/tendentious/Multiple-Choice-Questions-Android-App/tree/master/app/src/main/res/layout

问题活动:

https://github.com/tendentious/Multiple-Choice-Questions-Android-App/tree/master/app/src/main/java/com/Data

片段和测验活动:

https://github.com/tendentious/Multiple-Choice-Questions-Android-App/tree/master/app/src/main/java/com/QuizActivity

Apk文件路径:

https://github.com/tendentious/Multiple-Choice-Questions-Android-App/blob/master/app/build/outputs/apk/app-debug.apk

Existing format of the app

My requirement

请在这件事上给予我帮助...

我修改了Question.java和MyServerData.java文件,如下所示(用于添加正确的答案)

Question.java:

public class Question {
    private String question;
    private String answer;
    private String[] answerText;
    private Boolean[] correctAnswer;

    private Boolean[] checked = new Boolean[3];

    public Question(String question, String answer, String[] answerText, Boolean[] correctAnswer){
        this.question = question;
        this.answer = answer;
        this.answerText = answerText.clone();
        this.correctAnswer = correctAnswer.clone();
        this.resetChecked();
    }
    public void setChecked(int number,boolean state){
        if(number>=0 && number <=2){
            checked[number] = state;
        }
    }
    public void resetChecked(){
        Arrays.fill(checked,false);
    }
    public Boolean isChecked(int number) {
        return checked[number];
    }
    public Boolean isCorrectAnswer(int number){
        return  correctAnswer[number];
    }
    public Boolean[] getUserAnswers() {
        return checked;
    }
    public Boolean[] getAllCorrectAnswers(){
        return correctAnswer;
    }
    public String[] getAllAnswersText(){
        return answerText;
    }
    public String getQuestionText() {
        return question;
    }
    public String getAnswerText() {
        return answer;
    }
}

MyServerData.java

public class MyServerData {
    private static MyServerData myInstance = null;
    private static LinkedHashMap<String, Object> myCategories;
    private int totalQuestions;

    //test states: notStarted, inProgress, finished
    private static String testState = "notStarted";


    protected MyServerData() {
        //initialize questions from server, shared preferences, files etc.
        //questions are hardcoded here for testing
        Question[] FirstCategory  = new Question[3];;
        Question[] SecondCategory = new Question[3];;
        String q = "What is the capital of France ?";
        String b = "Paris";
        String[] a = new String[3];
        Boolean[] r = new Boolean[3];
        a[0] = "Paris";
        a[1] = "Prague";
        a[2] = "Berlin";
        r[0] = true;
        r[1] = false;
        r[2] = false;

        Question question = new Question(q,a,r);
        FirstCategory[0] = question;
        q = "Which of the following is an OS ?";
        b = "LINUX";
        a[0] = "PHP";
        a[1] = "LINUX";
        a[2] = "WINDOWS";
        r[0] = false;
        r[1] = true;
        r[2] = true;
        question = new Question(q,a,r);
        FirstCategory[1] = question;
        q = "What number is the highest ?";
        b = "18";
        a[0] = "12";
        a[1] = "14";
        a[2] = "18";
        r[0] = false;
        r[1] = false;
        r[2] = true;
        question = new Question(q,a,r);
        FirstCategory[2] = question;
        q = "Which of the following is flying ?";
        b = "Bird";
        a[0] = "Bird";
        a[1] = "Lion";
        a[2] = "Bee";
        r[0] = true;
        r[1] = false;
        r[2] = true;
        SecondCategory[0] = new Question(q,a,r);
        q = "What material is the strongest ?";
        b = "Diamond";
        a[0] = "Wood";
        a[1] = "Metal";
        a[2] = "Diamond";
        r[0] = false;
        r[1] = false;
        r[2] = true;
        SecondCategory[1] = new Question(q,a,r);q = "What number is the lowest ?";
        b = "11";
        a[0] = "15";
        a[1] = "11";
        a[2] = "12";
        r[0] = false;
        r[1] = true;
        r[2] = false;
        SecondCategory[2] = new Question(q,a,r);

        myCategories = new LinkedHashMap<String,Object>();
        myCategories.put("FirstCategory", FirstCategory);
        myCategories.put("SecondCategory", SecondCategory);
        for(String entry: myCategories.keySet()){
            totalQuestions += ((Question[])myCategories.get(entry)).length;
        }
    }

最后,我像这样修改了QuestionFragment.java:

public class QuestionFragment extends Fragment {

    LinearLayout answersContainer;
    int currentPageNr;

    @Override
    public View onCreateView(LayoutInflater lInflater, ViewGroup container,Bundle saveInstanceState){
        currentPageNr = getArguments().getInt("position");
        //initialize some variables
        final Question currentQuestion = MyServerData.getInstance().getQuestion(currentPageNr);
        View rootView = lInflater.inflate(R.layout.quiz_activity_fragment,container,false);
        //initialize questionText
        TextView question = (TextView) rootView.findViewById(R.id.questionText);
        question.setMovementMethod(new ScrollingMovementMethod());
        question.setText(currentQuestion.getQuestionText());

        //initialize correct answer from
        String correctAnswer = currentQuestion.getAnswerText();

        //initialize user input
        EditText et1 = (EditText) rootView.findViewById(R.id.et1);
        EditText et2 = (EditText) rootView.findViewById(R.id.et2);
        String input = et1.getText().toString().trim();

        //comparing both the strings
        if(MyServerData.getInstance().getTestState().equals("finished") ){
            et1.setEnabled(false);
            et2.setEnabled(false);
            if(correctAnswer.matches(et1.getText().toString().trim|())) {
                et1.setText(input)
                et1.setTextColor(Color.parseColor("#4DAD47"));
                et2.setText("Correct Answer: " + correctAnswer)             
            }else {
                et1.setText(input)
                et1.setTextColor(Color.parseColor("#CE0B0B"));
                et2.setText("Correct Answer: " + correctAnswer)             
            }

        }

        //removed some code
        return rootView;
    }
}

现在edittext2已正确实现

et2.setText("Correct Answer: " + correctAnswer) 

但是用户在edittext 1中输入的值将被清除。

String input = et1.getText().toString().trim();

et1.setText(input)