2013-02-17 72 views
0

即時通訊開發一個測驗,其中在java類中,我想要在不使用startactivity的情況下單擊下一個按鈕之後再打開另一個類(問題),還是有可能使用另一種方法調用另一個活動來傳遞另一個活動?請幫助我們......非常感謝你!我希望我的問題清楚..我真的很感謝你的幫助!如何在不調用android中的startactivity的情況下執行intent活動?

Question1.java

public class Question1 extends Activity implements OnClickListener 
{ 
int Scorecount = 0; 
//private RadioGroup rgp; 
private RadioButton rb1; 
private RadioButton rb2; 
private RadioButton rb3; 
private Button b1; 
//private TextView t1; 
int currentQuestion = 0; 
Toast t; 



@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.startquiz); 

Scorecount = getIntent().getIntExtra("score",0); 
rb1=(RadioButton)findViewById(R.id.option1); 
rb2=(RadioButton)findViewById(R.id.option2); 
rb3=(RadioButton)findViewById(R.id.option3); 
b1=(Button)findViewById(R.id.selected); 
b1.setOnClickListener(this); 
//rgp=(RadioGroup)findViewById(R.id.QueGroup1); 


//t1=(TextView)findViewById(R.id.txtdisplayanswer); 
} 

@Override 
public void onClick(View v) { 


    if(v == b1) 

    { 

     if(rb1.isChecked() || rb2.isChecked() || rb3.isChecked()) 
     { 
      if(rb1.isChecked()) 
      { 
       Scorecount++; 
       Toast.makeText(getApplicationContext(),"Your answer is correct!", 
         Toast.LENGTH_LONG).show(); 

       /*LinearLayout layout = (LinearLayout) t.getView(); 
       layout.setGravity(Gravity.CENTER); 
       layout.setBackgroundResource(R.drawable.toastgreen); 
      //t1.setText("Your answer is correct!" +rb1.getText());*/ 

      } 
     /* 
     if(rb2.isChecked() == true) 
      //t1.setText("Your wrong, the correct answer is: "+rb1.getText()); 
     if(rb3.isChecked() == true) 
      //t1.setText("Your wrong, the correct answer is: "+rb1.getText());*/ 

    else { 
     // do nothing 

     Toast.makeText(getApplicationContext(),"Your answer is wrong! The correct answers is: " + rb1.getText(), 
       Toast.LENGTH_LONG).show(); 

     /*LinearLayout layout = (LinearLayout) t.getView(); 
     layout.setGravity(Gravity.CENTER); 
     layout.setBackgroundResource(R.drawable.toastred);*/ 

    } 
      //Intent i = new Intent(this,Question2.class); 
      Intent i= getIntent(); 
      i.putExtra("score", Scorecount); 
      startActivity(i); 
      finish(); 


     } 

    } 

} 
+0

據我所知,你想每一個問題都是一個單一的活動,我認爲這不是最好的實現。我會考慮使用ViewPager並填充數據庫或某種數組中的問題。 – hardartcore 2013-02-17 09:30:43

+0

爲什麼你不要在** ** **活動中替換問題的文本和可能的答案。你不需要爲此創建另一個活動。 – 2013-02-17 11:49:10

+0

絕對同意前評論者的評論。您需要找到更通用的解決方案。例如,在數據庫中存儲問題文本和正確答案的編號(如果這些答案只是多選單個答案問題)。現在基於當前問題的id在活動中查詢數據庫(使用Loader)。 – 2013-02-17 13:03:44

回答

0

嘗試使用片段而不是活動,而且你不必使用開始活動爲每個新fragment.To改變你的屏幕只是做交易刪除舊片段並添加新片段。與使用多個活動來顯示每個不同的問題相比,此機制也更具有記憶效率。

相關問題