0

私の要件

で質問と回答を取っていString[]ます。TextViewには質問があり、RadioButton対応する回答があります。をクリックするStart Buttonと、最初の質問と回答のセットが に表示されLinear Layout1ます。Next Buttonがクリックされた場合はLinearLayout1次の一連の質問に置き換えられ、もう一度Next Buttonクリックされた場合は次の一連の質問が置き換えられ、その逆も同様です。のコードを書くのを手伝ってくださいNext Button

LinearLayout 1:クリック時のみ変更してくださいNext Button

コード:

TextView tv;
RadioButton rb1, rb2;
Button bStart, bNext;
String question[] = { "Key Lime Pie is upcoming version",
        "Android is Middleware", "Which is latest Android version?" };
String answerA[] = { "True", "False" };
String answerB[] = { "True", "False" };
String answerC[] = { "Android 4.2", "Android 5" };

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ques);
    tv = (TextView) findViewById(R.id.textView111);
    rb1 = (RadioButton) findViewById(R.id.radioButton1);
    rb2 = (RadioButton) findViewById(R.id.radioButton2);

    bStart = (Button) findViewById(R.id.startExam);
    bNext = (Button) findViewById(R.id.bNext);
    bStart.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            tv.setText(question[0]);
            rb1.setText(answerA[0]);
            rb2.setText(answerA[1]);
        }
    });
    bNext.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            tv.setText(question[1]);
            rb1.setText(answerB[0]);
            rb2.setText(answerB[1]);

        }
    });
}
4

2 に答える 2

0

回答用の単一の配列を作成します。そして、質問と回答の追跡変数を次のように宣言します。

  String answer[] = {"True", "False" , "True", "False", 
                     "Android 4.2", "Android 5" };
  int current = 0; // this is your new variable
  int ans = 1;     // this is initialized to 1, because 
                   // I assumed your first question and answer
                   // is displayed with the press of start button

次のボタンリスナーをこれに変更します

public void onClick(View v) {
        current ++;
        if(current < question.length){
            tv.setText(question[current]);
            rb1.setText(answer[ans+=1]);
            rb2.setText(answer[ans+=1]);
        }

    }
于 2012-11-21T16:28:42.707 に答える
0

これは論理的な問題です。

  1. すべての回答を多次元配列にマージします。
  2. 現在の質問を追跡します。
  3. 追跡変数を使用して、の質問タイトルを取得しquestionます。
  4. 追跡変数を使用して、回答の多次元配列で回答を取得します。これは、配列の最初の次元です。
  5. その次元の答えを読み、とに表示しrb1ますrb2

そこにあるものを使用すると、そのコードを拡張する際に問題が発生します。結局のところ、あなたは本当に答えの異なるセットごとに新しい変数を作成するつもりですか?それは意味がありません。多次元配列が機能します。

ただし、配列構造を改善し、を使用しintて現在表示されている質問を追跡することはできますが(したがって、を押した後に次の質問を取得するためにインクリメントすることもできます)、クラスbNextなど、より意味のあるオブジェクトを作成することをお勧めします。、Question関連するメンバー(title、、、、何でも...)、特にこのコードを使用してクイズスタイルのアプリケーションをプログラムする方法を学習しているように見えるため。optionAoptionB

次に、さまざまな質問を作成して、それを繰り返すことができます。

しかし、それはそこの問題ですか?いいえ。問題は質問と回答を追跡することです。追跡変数を持つ多次元配列を使用します。

//編集: OK、私はあなたのために非常に迅速な何かを提案するつもりです:

static String[] questions = new String[] { "Question 1", "Question 2", "Question 3" };
static String[][] answers = new String[][] {
        {"Answer 1.a", "Answer 1.b"},
        {"Answer 2.a", "Answer 2.b"},
        {"Answer 3.a", "Answer 3.b"},
    };
int pos = -1;

void showNextAnswer() {
    pos++;
    tv.setText(questions[pos]);
    rb1.setText(answers[pos][0]);
    rb2.setText(answers[pos][1]);
}

bStart.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        showNextAnswer();
    }
});

bNext.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        showNextAnswer();
    }
});

できる限り、両方のボタンは同じことを行うので、次のボタンのみを使用する方がおそらく良いでしょう。

于 2012-11-21T16:33:43.000 に答える