2011-02-01 5 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); 
} 

내가 코딩 side..I에 그 처리 제안은 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 측이 chieve 할 수있을 것입니다 의심!

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

다시 한번 고마워하지만 나에게 효과가있는 것 같지 않아 randomInt를 int randomInt = (int) (Math.random() * 1000); 이클립스가 내놓은 오류를 해결하기 위해 앱을 실행할 때 버튼이 같은 위치에있다. 내가해야 할 일이 있었나요? 다시 한 번 감사드립니다! – Cam

+0

당신은 한가지 더 할 수 있습니다. 카운터를 가져 와서 간단히 증가 시키십시오 ... 난수 대신 – garima