2015-04-03 41 views
0

我有一個場景,用戶參加一個有40多個問題的測驗。創建40多項活動是一項繁瑣的任務&我想知道是否有任何東西存在重用?爲Quizz應用程序中的每個任務重新使用UI?

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" 
    > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:text="Who founded Apple?" 
     android:id="@+id/textView" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="49dp" /> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/editText" 
     android:text="Fill answers here" 
     android:layout_below="@+id/textView" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="37dp" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Next" 
     android:id="@+id/button" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@+id/editText" 
     android:layout_toEndOf="@+id/editText" 
     android:clickable="true" /> 
</RelativeLayout> 

在我MainActivity.class,即時常做這樣

Button bt = (Button) findViewById(R.id.button); 

     bt.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       // ....Go to next activity through Intent... 
      } 
     }); 

看看裏面的onClick,我需要有一點都不好每一個問題的每個活動。

任何其他解決方案?

+0

你是從數據庫中提取這些問題文本? – Ajeet 2015-04-03 18:28:28

+3

你是對的,40個活動將是愚蠢的。但是,如果你處於如何做到這一點甚至是一個問題的階段,你需要思考如何做事,而不是用勺子喂答案。考慮一下 - 每個問題需要哪些數據?你如何改變UI來顯示這些數據?我什麼時候需要這樣做?一旦你弄清楚這些事情,應該有一個明顯的前進方向。 – 2015-04-03 18:28:33

+0

@Ajeet:是的,來自數據庫。 – user3289108 2015-04-03 18:29:46

回答

0

如果沒有,您可能希望爲您的測驗創建數據結構。一旦你有了一個數據結構,你就可以考慮你想要使用什麼類型的適配器:也就是說,將你的測試集與UI相關聯的機制。例如,當需要顯示某個列表時,listview與ArrayAdapter(或BaseAdapter用於更多自定義樣式的ListView)一起使用。

在支持庫中有一種稱爲viewpager的東西,它是一組您可以在其間水平滑動的頁面。你可以考慮使用這個。

或者您可以創建自己的視圖類,它具有顯示測驗所需的所有視圖,添加如switchQuiz(Quiz q)這樣的方法,它可以用新測驗信息更改ui,並在單個活動中使用此視圖。具體而言,您可以採用activity_main.xml佈局文件,而不是將其用作活動的佈局,請創建一個自定義視圖(public class QuizView extends RelativeLayout { }),該視圖可以更新交換機測驗功能的ui。在這裏閱讀有關如何做到這一點的詳細信息:http://trickyandroid.com/protip-inflating-layout-for-your-custom-view/

0

創建兩個按鈕,prev和next ..

上一張帶你上一個問題,接下來帶你下一個問題

創建包含一類問題以下變量

int number_of_option; 
    String[] options; 
    String question; 
    String answer; 

創建包含ArrayList<question> list; 添加必需的方法來添加問題,讓問題一類測驗,更新answer..etc..etc

在測驗類中創建變量current_position_in_UI以在活動中顯示該問題。

在next/prev按鈕上單擊調用測驗方法以獲取問題字段。像list.get(位置),並更新您的意見與這些調用的值。同時更新你的current_position_in_UI。希望它有助於:)

相關問題