首页 > 解决方案 > 如何在此徽标测验中添加更多随机问题?

问题描述

我本周创建了一个徽标测验应用程序,但这个徽标测验的问题在于它只有一个问题。回答后,测验结束。我想知道如何在此测验中添加更多问题以及如何随机显示问题。我的意思是,在回答完问题后,出现了另一张随机图片。我是 android 开发的新手,我仍然需要其他开发人员的帮助,我相信这是最好的提问地点。非常感谢您提前帮助我。

public class MainActivity extends AppCompatActivity {
private int presCounter = 0;
private int maxPresCounter = 5;
private String[] keys = {"S", "A", "M", "P", "L"};
private String textAnswer = "SAMPL";
TextView textScreen, textQuestion, textTitle;
@Override
protected void onCreate....
    keys = shuffleArray(keys);
    for (String key : keys) {
        addView(((LinearLayout) findViewById(R.id.layoutParent)), key, ((EditText) findViewById(R.id.editText)));
    }
    maxPresCounter = 5;
}


private String[] shuffleArray(String[] ar) {
    Random rnd = new Random();
    for (int i = ar.length - 1; i > 0; i--) {
        int index = rnd.nextInt(i + 1);
        String a = ar[index];
        ar[index] = ar[i];
        ar[i] = a;
    }
    return ar;
}


private void addView(LinearLayout viewParent, final String text, final EditText editText) {
    LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT
    );

    linearLayoutParams.rightMargin = 30;

    final TextView textView = new TextView(this);

    textView.setLayoutParams(linearLayoutParams);
    textView.setBackground(this.getResources().getDrawable(R.drawable.pix));

    textQuestion = (TextView) findViewById(R.id.textQuestion);
    textScreen = (TextView) findViewById(R.id.textScreen);
    textTitle = (TextView) findViewById(R.id.textTitle);
    textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(presCounter < maxPresCounter) {
                if (presCounter == 0)
                    editText.setText("");
                    editText.setText(editText.getText().toString() + text);
                    presCounter++;
                    if (presCounter == maxPresCounter)
                        doValidate();
            }
        }
    });

    viewParent.addView(textView);
}
private void doValidate() {
    presCounter = 0;
    EditText editText = findViewById(R.id.editText);
    LinearLayout linearLayout = findViewById(R.id.layoutParent);
    if(editText.getText().toString().equals(textAnswer)) {
     Toast.makeText(MainActivity.this, "Correct", Toast.LENGTH_SHORT).show();


        editText.setText("");
    } else {
        Toast.makeText(MainActivity.this, "Wrong", Toast.LENGTH_SHORT).show();
        editText.setText("");
    }
    keys = shuffleArray(keys);
    linearLayout.removeAllViews();
    for (String key : keys) {
        addView(linearLayout, key, editText);
    }
}
}

标签: javaandroidarrays

解决方案


推荐阅读