2010-05-31 31 views
8

Tôi đã 16 TextView và cần phải thiết lập một cái gì đó như thế này thực hiện:Làm cách nào để lặp qua các thuộc tính id của lớp R.java?

for (int i=1; i<6; i++) 
{ 
    int $RidInt = R.id.s; 
    tv[i] = (TextView)findViewById($RidInt); 
    tv[i].setTypeface(face); 
    tv[i].setClickable(true); 
    tv[i].setOnClickListener(clickListener); 
} 

tập tin R.java của tôi là:

public final class R { 
    public static final class attr { 
    } 
    public static final class drawable { 
     public static final int icon=0x7f020000; 
    } 
    public static final class id { 
     public static final int s1=0x7f050000; 
     public static final int s10=0x7f050009; 
     public static final int s11=0x7f05000a; 
     public static final int s12=0x7f05000b; 
     public static final int s13=0x7f05000c; 
     public static final int s14=0x7f05000d; 
     public static final int s15=0x7f05000e; 
     public static final int s16=0x7f05000f; 
     public static final int s2=0x7f050001; 
     public static final int s3=0x7f050002; 
     public static final int s4=0x7f050003; 
     public static final int s5=0x7f050004; 
     public static final int s6=0x7f050005; 
     public static final int s7=0x7f050006; 
     public static final int s8=0x7f050007; 
     public static final int s9=0x7f050008; 
    } 
    public static final class layout { 
     public static final int main=0x7f030000; 
     public static final int toast=0x7f030001; 
    } 
    public static final class string { 
     public static final int app_name=0x7f040000; 
     public static final int s2=0x7f040001; 
    } 
} 

Trả lời

13

Something như vậy?

import java.lang.reflect.Field; 
/* ... */ 

for (int i = 1; i < 16; i++) { 
    int id = R.id.class.getField("s" + i).getInt(0); 
    tv[i] = (TextView)findViewById(id); 
    tv[i].setTypeface(face); 
    tv[i].setClickable(true); 
    tv[i].setOnClickListener(clickListener); 
} 
+0

Hoàn hảo! Điều đó hiệu quả. Cảm ơn. –

+0

Thực sự làm việc tốt đẹp. !! – MKJParekh

+0

vì một số lý do tôi cần thêm khối try/catch - nhưng hoạt động rất đẹp! anyway để tránh thử/bắt? – Jona

2

dùng thử.

public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.main); 
      LinearLayout ll = (LinearLayout) findViewById(R.id.LinearLayout01); 
      for (int i = 0; i < ll.getChildCount(); i++) { 
       ((TextView) ll.getChildAt(i)).setText("Text View " + i); 
      } 
+0

Tôi nghĩ rằng điều này sẽ ném rất nhiều CastingExpcetions nếu không phải tất cả các trẻ em là văn bản – RoflcoptrException

+0

mgpyone hỏi "Tôi đã 16 textViews và cần phải thiết lập một cái gì đó như thế này thực hiện:". bạn có thể đặt tất cả các bản xem trước trong một bố cục tuyến tính. và tôi chỉ đưa ra một gợi ý rằng đây cũng là một cách để đạt được yêu cầu. – Vivart

2

Tôi phải đóng gói của tôi trong trình bao bọc thử/bắt, không chắc chắn tại sao, nhưng tôi rất mới.

for (int counter = 1; player < 5; counter++) { 
     try {Field field = example.main.R.id.class.getField("s" + counter); 
      try {screenid = field.getInt(null);} 
      catch (IllegalAccessException e){//exceptioncodehere} 
     catch (NoSuchFieldException e) {//exception2codehere} 
     TextView SetMe = (TextView)findViewById(screenid); 
     SetMe.setText("Text" + player);}} 
Các vấn đề liên quan