2013-05-03 67 views
0

我正在製作一個文字遊戲,其中每個用戶都有多個猜測,每個猜測都由多個TextView組成。到目前爲止,我的代碼讀取:android textview arrays

TextView[] guess1 = new TextView[numTextViews]; 
guess1[0] = (TextView) findViewById(R.id.Guess1_1); 
guess1[1] = (TextView) findViewById(R.id.Guess1_2); 
guess1[2] = (TextView) findViewById(R.id.Guess1_3); 
guess1[3] = (TextView) findViewById(R.id.Guess1_4); 
guess1[4] = (TextView) findViewById(R.id.Guess1_5); 

與XML看起來像:

<TextView 
    android:id="@+id/Guess1_1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:gravity="center" 
    android:text="@string/guessChar" />... 

android:id=改變重複。

如果我輸入TextView[] guess2及其所有元素,我將重複自己。

  • 什麼是更好的方式去做這件事?
  • 以編程方式創建所有TextView是否更好?因爲它們非常相似?
+0

你所做的事對我來說似乎很好。使用XML更好。 – Doomsknight 2013-05-03 06:55:03

回答

0

你知道每個文本視圖的猜測量是多少?

我會建議你使用反射

Class clazz = R.id.class; // get the R class 
Field f = clazz.getField("Guess1_" + "1"); 
int id = f.getInt(null); // pass in null, since field is a static field. 
TextView currcell = (TextView) findViewById(id); 

在這種情況下,將案件帶來Guess1_1

你:

for (int i =0; i < numTextViews; i++) 
{ 
    Class clazz = R.id.class; 
    Field f = clazz.getField("Guess1_" + Integer.toString(i+1)); 
    int id = f.getInt(null); 
    guess[i] = (TextView)findViewById(id); 
} 

但這隻能帶給你的第一陣列Guess1你需要將其轉換爲通用代碼.. 所以可能會發生一些問題..所以用xml讀取它,因爲你現在將是最簡單的方法..

編輯:

如果所有的TextView具有相同的屬性,你也可以創建編程

LinearLayout view = new LinearLayout(this); // create new linear layout 
view.setOrientation(LinearLayout.HORIZONTAL); // optional.. so the 
               // view will be horizontaly 

view.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, 
         LinearLayout.LayoutParams.WRAP_CONTENT)); // set the layout   
                    // height and width 

for (int i = 0; i < numOf ; i ++)          
{      
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.WRAP_CONTENT, 
       LinearLayout.LayoutParams.WRAP_CONTENT); 

    guess[i] = new TextView(); 
    guess[i].setLayoutParams(lp); 
    guess[i].setID(i+1); 
} 
+0

而不是反射,使用Resources.getIdentifier更具可讀性 – njzk2 2013-05-03 07:48:28

+0

是的我知道,但它只是一個建議.. :) – Elior 2013-05-03 08:16:54

+0

鑑於資源中存在的getIdentifier,我不明白爲什麼有人會使用反射來找到一個id? – njzk2 2013-05-03 08:21:13

3

這是你如何可以通過你的看法迭代沒有重複的代碼使用的id :

LinearLayout ll = (LinearLayout) findViewById(R.id.layout_containing_textviews); 
for (int i = 0; i < ll.getChildCount(); i++) { 
    if (ll.getChildAt(i).getClass() == TextView.class) { 
     guess1[i] = (TextView)ll.getChildAt(i); 
    } 
} 

確保的情況下,調整這個你有因爲非TextView意見在這種情況下,索引將不連續。您可以爲TextView s使用另一個計數器。

現在如果你的佈局只有TextView s,你甚至不需要數組。您可以將該佈局作爲容器/數組的使用方式在上面的剪切中使用。

0

你既可以創建編程的textViews(和使用,如果你想使用一些XML過於膨脹),或者你可以使用則getIdentifier方法,例如:如果你希望做

private static final String ID_FORMAT="Guess1_%d"; 
... 
for(int i=0;i<10;++i) 
    { 
    String id=String.format(FORMAT,i); 
    TextView tv = (TextView) findViewById(getResources().getIdentifier(id, "id", getPackageName())); 
    //... 
    } 

也一樣循環內的循環。

如果佈局有很多視圖,我會建議使用adapterView(listView,gridView,...)來代替,並避免創建如此多的視圖(以編程方式或通過xml)。