若有發現錯誤記得留言告知哦~~我會很感激的!!

佈局文件:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:id="@+id/LinearLayout01" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent"> 
  6.  
  7.     <TextView 
  8.         android:id="@+id/title" 
  9.         android:layout_width="wrap_content" 
  10.         android:layout_height="wrap_content" 
  11.         android:layout_alignParentLeft="true" 
  12.         android:layout_alignParentTop="true" 
  13.         android:text="練習動態產生元件" 
  14.         android:textAppearance="?android:attr/textAppearanceLarge" /> 
  15.  
  16.     <CheckBox 
  17.         android:id="@+id/c1" 
  18.         android:layout_width="wrap_content" 
  19.         android:layout_height="wrap_content" 
  20.         android:layout_alignParentLeft="true" 
  21.         android:layout_below="@+id/title" 
  22.         android:text="個性" /> 
  23.  
  24.     <CheckBox 
  25.         android:id="@+id/c2" 
  26.         android:layout_width="wrap_content" 
  27.         android:layout_height="wrap_content" 
  28.         android:layout_alignParentLeft="true" 
  29.         android:layout_below="@+id/c1" 
  30.         android:text="勢力名稱" /> 
  31.  
  32.     <CheckBox 
  33.         android:id="@+id/c3" 
  34.         android:layout_width="wrap_content" 
  35.         android:layout_height="wrap_content" 
  36.         android:layout_alignParentLeft="true" 
  37.         android:layout_below="@+id/c2" 
  38.         android:text="顏色" /> 
  39.  
  40.     <Button 
  41.         android:id="@+id/b1" 
  42.         android:layout_width="match_parent" 
  43.         android:layout_height="wrap_content" 
  44.         android:layout_alignParentLeft="true" 
  45.         android:layout_alignParentRight="true" 
  46.         android:layout_below="@+id/c3"  
  47.         android:text="ok" 
  48.         android:onClick="start" /> 
  49.         <!-- 寫android:onClick="start"就不用大費周章的再宣告此按鈕的監聽器 --> 
  50. </LinearLayout> 

主程式:

  1. //學習目的:動態產生物件、CheckBox用法、Array(容器)用法 
  2. package my.practice; 
  3.  
  4. import android.os.Bundle; 
  5. import android.app.Activity; 
  6. import android.view.Menu; 
  7. import android.view.View; 
  8. import android.widget.Button; 
  9. import android.widget.CheckBox; 
  10. import android.widget.LinearLayout; 
  11. import android.widget.TextView; 
  12.  
  13. public class MainActivity extends Activity { 
  14.     LinearLayout LL; 
  15.     CheckBox c1,c2,c3; 
  16.     Button b1; 
  17.      
  18.     TextView tv[]=new TextView[12]; 
  19.     //創造只能裝TextView的容器(容器名稱:tv),裏面留有12個擺放空間 
  20.      
  21.     String sName[]={//裝人名的容器sName,其sName[n]為裏面裝的實體物件 
  22.             "■曹操"
  23.             "■劉備"
  24.             "■孫權"}; 
  25.     String sComment[]={//裝個性的容器sComment 
  26.             "-治世之能臣,亂世之奸雄"
  27.             "-仁义其表,峥嵘其心"
  28.             "-生子当如孙仲谋"}; 
  29.     String sAddress[]={//裝地域的容器sAddress 
  30.             "-魏國"
  31.             "-蜀國"
  32.             "-吳國"}; 
  33.     String sColor[]={//裝顏色的容器sColor 
  34.             "-藍色"
  35.             "-綠色"
  36.             "-紅色"}; 
  37.     @Override 
  38.     public void onCreate(Bundle savedInstanceState) { 
  39.         super.onCreate(savedInstanceState); 
  40.         setContentView(R.layout.activity_main); 
  41.          
  42.         LL=(LinearLayout)findViewById(R.id.LinearLayout01); 
  43.         //想要动态产生物件就必須先創造一個Layout當作容器裝物件。 
  44.          
  45.         c1=(CheckBox)findViewById(R.id.c1); 
  46.         c2=(CheckBox)findViewById(R.id.c2); 
  47.         c3=(CheckBox)findViewById(R.id.c3); 
  48.          
  49.         for(int i=0;i<12;i++){ 
  50.             tv[i]=new TextView(this);//實體化TextView物件     
  51.             LL.addView(tv[i]);//把物件裝進容器裏,再由容器顯現到Activity 
  52.         } 
  53.     } 
  54.     public void start(View view){//搭配佈局文件,按鈕按下時動態產生TextView 
  55.         int index=0;//控制TextView容器空間的編號 
  56.         for(int j=0;j<3;j++){ 
  57.         //tv容器裏的index個位子, 
  58.         //放進sName或sComment或sAddress或sColor的j個string 
  59.              
  60.             tv[index].setText(sName[j]);//確保每一組第一個出現的是人名 
  61.              
  62.             index++;//接著下一個tv容器空間的號碼 
  63.              
  64.             if(c1.isChecked()){//假如點選個性 
  65.                  
  66.                 tv[index].setText(sComment[j]); 
  67.                 //tv[index]=選擇tv容器裏的第index個物件, 
  68.                 //.setText(sComment[j])=設定它的文字為sComment容器裏第j個物件 
  69.                  
  70.                 index++;////接著下一個tv容器空間的號碼 
  71.             } 
  72.             if(c2.isChecked()){ 
  73.                 tv[index].setText(sAddress[j]); 
  74.                 index++; 
  75.             } 
  76.             if(c3.isChecked()){ 
  77.                 tv[index].setText(sColor[j]); 
  78.                 index++; 
  79.             } 
  80.         } 
  81.     } 
  82.     @Override 
  83.     public boolean onCreateOptionsMenu(Menu menu) { 
  84.         getMenuInflater().inflate(R.menu.activity_main, menu); 
  85.         return true
  86.     } 

 程式界面:

android動態產生物件、CheckBox、Array、android:onClick_CheckBox