2013-07-05 26 views
-1

我正在測試應用程序。我現在編碼並且對第一個問題起作用。但我不知道如何去解決第二個問題。我試圖使用while-do循環和其他的東西來做到這一點,但沒有奏效。我能做什麼?任何人都可以幫我嗎?如何跳轉到Android測試應用程序的下一部分?

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]);} 
    } 
     });} 
} 
+0

'我能做什麼?'用什麼? 「有人能幫我嗎?」什麼?對於FSM來說,問題在哪裏?什麼不工作? – Selvin

回答

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

其中FromActivity是您的Cu的名字rrent java文件和ToActivity是您的新java文件的名稱,忽略.java擴展名

I.e. 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"對於靜態名稱

+0

但我想隨機生成很多問題。所以我不能使用所有這些新的活動。所有人都必須處於不活動狀態。 –

+0

啊對了,對不起,我沒有意識到/看到你想要隨機提問......你能從數據庫中加載問題嗎?如果存儲在手機上的SQLite數據庫?這也可以很容易地讓你在以後更新的問題,而不必改變整個應用程序... –

+0

看看這篇文章的SQLite隨機選擇的詳細信息... http://stackoverflow.com/questions/2279706/select-random-row-from-an-sqlite-table –

0

鑑於您使用相同的視圖來指示答案的正確性,您需要一些額外的觸發器 - 我建議將「下一步」按鈕添加到您的佈局。

然後,將用於設置問題視圖(選擇國家和答案資源)的代碼片段分解爲以問題編號爲輸入的方法void setUpQuestion(int questionIndex)。使用作爲輸入到setUpQuestion()

OnClick()方法「下一步」按鈕增量currentQuestion創建memver vaiable int currentQuestion = 0並呼籲setUpQuestion()更新的觀點。

+0

哦,非常感謝你的回答。 –

相關問題