从数组android限制显示的问题

时间:2014-08-26 12:14:32

标签: android

我想将我的测验设置为仅显示三个问题,并且在得到正确答案之前不会继续进行下一个问题。这是我当前的代码

顺便说一下,我的申请是这样的,

  • 问题的Textview
  • Textfield for Answer
  • 按钮检查答案是否正确/不正确

    public class ArrayAct extends Activity {
    
    private Button doneBtn;
    private EditText text;
    private TextView textView;
    private String [] mArray;
    private String [] mArray1;
    private int generatedIndex;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.arrayact);
    
        textView = (TextView)findViewById(R.id.txtView);
        doneBtn = (Button)findViewById(R.id.doneBtn);
        text = (EditText)findViewById(R.id.txtFld);
    
        mArray = getResources().getStringArray(R.array.Answers);
        mArray1 = getResources().getStringArray(R.array.Questions);
    
        doneBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (mArray[generatedIndex].equals(text.getText().toString()))
                    Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();
                else
                    Toast.makeText(getApplicationContext(), "Incorrect.Please try again.", Toast.LENGTH_SHORT).show();
    
            }
        });
    
        Random random = new Random();
    
        generatedIndex = random.nextInt(mArray1.length);
        textView.setText(mArray1[generatedIndex]);
    }
    }
    

2 个答案:

答案 0 :(得分:1)

请尝试这种方式,希望这有助于您解决问题。

public class ArrayAct extends Activity {
    private Button doneBtn;
    private EditText text;
    private TextView textView;
    private String [] mArray;
    private String [] mArray1;
    private int generatedIndex;
    private HashMap<String,String> questionMap;

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

        textView = (TextView)findViewById(R.id.txtView);
        doneBtn = (Button)findViewById(R.id.doneBtn);
        text = (EditText)findViewById(R.id.txtFld);

        mArray = getResources().getStringArray(R.array.Answers);
        mArray1 = getResources().getStringArray(R.array.Questions);
        questionMap = new HashMap<String, String>();

        doneBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if(doneBtn.getText().equals("Close")){
                    finish();
                }else{
                    if (mArray[generatedIndex].equals(text.getText().toString())){
                        Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();
                        if(questionMap.size()==3){
                            Toast.makeText(getApplicationContext(), "Your test is over!!!", Toast.LENGTH_SHORT).show();
                            doneBtn.setText("Close");
                        }else{
                            text.setText("");
                            prepareQuestion();
                        }
                    }else{
                        Toast.makeText(getApplicationContext(), "Incorrect.Please try again.", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        prepareQuestion();
    }

    private void prepareQuestion(){
        Random random = new Random();
        do{
            generatedIndex = random.nextInt(mArray1.length);
        }while(questionMap.containsKey(String.valueOf(generatedIndex)));
        questionMap.put(String.valueOf(generatedIndex),"");
        textView.setText(mArray1[generatedIndex]);
    }
}

答案 1 :(得分:0)

doneBtn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        if (mArray[generatedIndex].equals(text.getText().toString())) {
            Toast.makeText(getApplicationContext(), "Correct!", Toast.LENGTH_SHORT).show();
            if(arrayOfUsedIndexes.size() == 3) {
                //Some reaction for all 3 questions answered correctly
            }
            else {
               int newGeneratedIndex;
               do {
                  newGeneratedIndex = random.nextInt(mArray1.length);
               } while(arrayOfUsedIndexes.contains(newGeneratedIndex))
               textView.setText(mArray1[newGeneratedIndex]);
               arrayOfUsedIndexes.add(newGeneratedIndex);
           }
       } 
       else {
         Toast.makeText(getApplicationContext(), "Incorrect.Please try again.",    Toast.LENGTH_SHORT).show(); }
  }
});

Random random = new Random();

generatedIndex = random.nextInt(mArray1.length);
arrayOfUsedIndexes.add(generatedIndex);
textView.setText(mArray1[generatedIndex]);