随机放置按钮

时间:2011-02-01 03:57:53

标签: android

我有一个简单的问答游戏,它运作良好,但我遇到的一个问题是问题的答案和选项显示在按钮上,但答案按钮每次出现在同一个地方。

有没有办法通过XML接口随机化定位,还是在编码方面处理这个更好?

下面是我用于XML的代码以及用于将数据值从SQLite绑定到按钮的代码。任何建议都将不胜感激。

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 个答案:

答案 0 :(得分:1)

您好我建议在编码方面处理..我怀疑我能在xml方面解决这个问题!

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

你可以尝试各种随机方法以各种方式显示!!我希望  这有助于