2011-02-01 15 views
0

私はうまく動作している単純なクイズゲームを持っていますが、質問の答えとオプションはボタンに表示されますが、回答ボタンは毎回同じ場所に表示されます。ボタンのランダム化配置

XMLインターフェイスを使用して位置をランダム化する方法はありますか、それともコーディング側でこれを処理する方がよいでしょうか?

以下は、SQLiteからボタンにデータ値をバインドするために使用するXMLとコードに使用しているコードです。アドバイスをいただければ幸いです。

private void showQuestions(Cursor cursor) { 
// Collect String Values from Query 
StringBuilder questiontxt = new StringBuilder(""); 
StringBuilder answertxt1 = new StringBuilder(""); 
StringBuilder answertxt2 = new StringBuilder(""); 
StringBuilder answertxt3 = new StringBuilder(""); 
StringBuilder answertxt4 = new StringBuilder(""); 

while (cursor.moveToNext()) { 
String question = cursor.getString(2); 
String answer = cursor.getString(3); 
String option1 = cursor.getString(4); 
String option2 = cursor.getString(5); 
String option3 = cursor.getString(6); 
questiontxt.append(question).append(""); 
answertxt1.append(answer).append(""); 
answertxt2.append(option1).append(""); 
answertxt3.append(option2).append(""); 
answertxt4.append(option3).append(""); 
} 
// Display answer button 
TextView answer = (TextView) findViewById(R.id.option1_button); 
answer.setText(answertxt1); 
//Display question 
TextView text = (TextView) findViewById(R.id.text); 
text.setText(questiontxt); 
TextView optionbutton1 = (TextView) findViewById(R.id.option2_button); 
optionbutton1.setText(answertxt2); 
TextView optionbutton2 = (TextView) findViewById(R.id.option3_button); 
optionbutton2.setText(answertxt3); 
TextView optionbutton3 = (TextView) findViewById(R.id.option4_button); 
optionbutton3.setText(answertxt4); 
} 

そしてXML

<Button 
android:id="@+id/option1_button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_above="@+id/option3_button"/> 

<Button 
android:id="@+id/option2_button" 
android:text="@string/option2_label" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignParentRight="true" 
android:layout_above="@+id/option4_button"/> 

<Button 
android:id="@+id/option3_button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/option3_label" 
android:layout_alignParentLeft="true" 
android:layout_alignParentBottom="true"/> 

<Button 
android:id="@+id/option4_button" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/option4_label" 
android:layout_alignParentRight="true" 
android:layout_alignParentBottom="true"/> 

答えて

1

こんにちは、私はあなたがXML側でこれを成し遂げることができるだろうコーディングside..Iの疑いでこれを扱うことをお勧め!

int randomInt = Math.random() * 1000; 

    TextView answer = (TextView) findViewById(R.id.option1_button); 
    answer.setText(answertxt1); 
    //Display question 
    TextView text = (TextView) findViewById(R.id.text); 
    text.setText(questiontxt); 
if(randomInt%3==0){ 
    TextView optionbutton1 = (TextView) findViewById(R.id.option2_button); 
    optionbutton1.setText(answertxt2); 
    TextView optionbutton2 = (TextView) findViewById(R.id.option3_button); 
    optionbutton2.setText(answertxt3); 
    TextView optionbutton3 = (TextView) findViewById(R.id.option4_button); 
    optionbutton3.setText(answertxt4);} 
else {TextView optionbutton1 = (TextView) findViewById(R.id.option3_button); 
    optionbutton1.setText(answertxt3); 
    TextView optionbutton2 = (TextView) findViewById(R.id.option4_button); 
    optionbutton2.setText(answertxt4); 
    TextView optionbutton3 = (TextView) findViewById(R.id.option2_button); 
    optionbutton3.setText(answertxt2);} 

さまざまな方法でさまざまな方法で表示できます。私は これが役立つことを願っています

+0

ありがとうGarimaこれをJavaで処理する最良のアプローチに関するアイデア? – Cam

+0

もう一度おねがいしますが、それは私のために働くようですdoes notはrandomIntをintに変更しましたrandomInt =(int)(Math.random()* 1000); Eclipseが出していたエラーを解決するために、私がアプリケーションを実行するとき、ボタンは同じ場所にとどまります。私は何かする必要があったのですか?再度、感謝します! – Cam

+0

あなたはもう1つのことをすることができます..カウンタを取って、単純にインクリメントしてください...乱数の代わりに – garima