限制从阵列的android显示的问题 [英] Limit the displayed questions from array android

查看:115
本文介绍了限制从阵列的android显示的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置我的测验,只显示三个问题,直到它得到正确的答案不继续到下一个。这是我目前的code

I want to set my quiz to only show three questions , and not proceeding to the next one until it gets the correct answer. Here's my current code

我的应用程序是这样的方式,

My application goes like this by the way,


  • 的TextView的问题

  • 文本字段的答案

  • 按钮来检查答案是正确/不正确

  • Textview for Question
  • Textfield for Answer
  • Button to check if answer is correct/incorrect

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


推荐答案

试试这个办法,希望这将帮助你解决你的问题。

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

这篇关于限制从阵列的android显示的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆