-1

我正在开发一个测试应用程序。我现在对其进行了编码,它对第一个问题很有用。但我不知道如何回答第二个问题。我尝试使用 while-do 循环和其他方法来做到这一点,但没有奏效。我能做些什么?任何人都可以帮助我吗?

public class testing extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testing);

        TextView view = (TextView) findViewById(R.id.soru1);
        final Button answer1 = (Button) findViewById(R.id.answer1);
        final Button answer2 = (Button) findViewById(R.id.answer2);
        final Button answer3 = (Button) findViewById(R.id.answer3);
        final Button answer4 = (Button) findViewById(R.id.answer4);

        final ArrayList<String> questions= new ArrayList<String>();
        countries.add("question1");
        countries.add("question2");
        countries.add("question3");
        countries.add("question4");
        countries.add("question5");
        countries.add("question6");
        countries.add("question7");
        countries.add("question8");

        final int[] answers= new int[]{
                R.drawable.pic1, 
                R.drawable.pic2,
                R.drawable.pic3,
                R.drawable.pic4,
                R.drawable.pic5,
                R.drawable.pic6,
                R.drawable.pic7,
                R.drawable.pic8,
                R.drawable.correct,
                R.drawable.wrong,

                };

        Random soru = new Random();
        final int[] rastgele = new int[1];
        for (int i=0; i<1; i++)
                {rastgele[i]= soru.nextInt(8);}

         ArrayList<Integer> cevap = new ArrayList<Integer>();          
         for (int k = 0; k <= 7; ++k) 
            {cevap.add(k);}
         Collections.shuffle(cevap);

         final Integer[] rastgele2 = new Integer[4];
                    if (rastgele[0]!=cevap.get(0))
                    {rastgele2[0]=cevap.get(0);}
                    else
                    {rastgele2[0]=cevap.get(3);}
                    if (rastgele[0]!=cevap.get(1))
                    {rastgele2[1]=cevap.get(1);} 
                    else
                    {rastgele2[1]=cevap.get(3);}
                    if (rastgele[0]!=cevap.get(2))
                    {rastgele2[2]=cevap.get(2);} 
                    else
                    {rastgele2[2]=cevap.get(3);}                    
                    rastgele2[3]=rastgele[0];
                    Collections.shuffle(Arrays.asList(rastgele2));  

        view.setText(questions.get(rastgele[0]));
        answer1.setBackgroundResource(answers[rastgele2[0]]);
        answer1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            if (rastgele[0]==rastgele2[0])
                {answer1.setBackgroundResource(answers[8]);
                questions.remove(rastgele[0]);}
            else {answer1.setBackgroundResource(answers[9]);}
                                        }
        });
        answer2.setBackgroundResource(answers[rastgele2[1]]); 
        answer2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

            if (rastgele2[1]==rastgele[0])
            {answer2.setBackgroundResource(answers[8]);
                countries.remove(rastgele[0]);}
            else {answer2.setBackgroundResource(answers[9]);}
                                        }
        });
        answer3.setBackgroundResource(answer[rastgele2[2]]);   
        answer3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (rastgele2[2]==rastgele[0])
            {answer3.setBackgroundResource(answer[8]);
                countries.remove(rastgele[0]);}
            else {answer3.setBackgroundResource(answer[9]);}
                                        }
        });
        answer4.setBackgroundResource(answer[rastgele2[3]]);
        answer4.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (rastgele2[3]==rastgele[0])
            {answer4.setBackgroundResource(answer[8]);
                countries.remove(rastgele[0]);}
            else {answer4.setBackgroundResource(answer[9]);}
    }
        });}
} 
4

2 回答 2

0

鉴于您使用相同的视图来指示答案的正确性,您需要一些额外的触发器 - 我建议在您的布局中添加“下一步”按钮。

然后,将设置问题视图(为答案选择国家和资源)的一段代码分解为void setUpQuestion(int questionIndex)以问题编号作为输入的方法。int currentQuestion = 0创建可用作输入的memversetUpQuestion()

OnClick()“下一步”按钮增量currentQuestion和调用setUpQuestion()更新视图的方法中。

于 2013-07-05T15:03:36.707 回答
0

要移动到新活动,请参阅这篇文章How to open a second Activity on click of button in android app

在您现有的布局中,您需要这个

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="28dp"
    android:onClick="RandomButtonNameHere"
    android:text="@string/button" />

然后在您现有的 JAVA 中,您想要启动一个新的活动意图 onclick

public void RandomButtonNameHere(View view) 
{
Intent intent = new Intent(FromActivity.this, ToActivity.class);
startActivity(intent);
}

whereFromActivity是当前 java 文件ToActivity的名称,是新 java 文件的名称,忽略 .java 扩展名

即 Question1.Java 到 Question2.Java 将是

public void ButtonToQuestion2(View view) 
{
Intent intent = new Intent(Question1.this, Question2.class);
startActivity(intent);
}

并由 Question1.xml 中的按钮调用

 <Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="28dp"
    android:onClick="ButtonToQuestion2"
    android:text="@string/ButtonToQuestion2" />

请注意,我的按钮名称是在我的字符串列表中定义的,但您可以将其重命名"@string/ButtonToQuestion2""Name of Button"静态名称

于 2013-07-05T14:57:04.823 回答