How do I iterate through the id properties of R.java class?

前端 未结 3 1659
有刺的猬
有刺的猬 2020-12-01 15:26

I\'ve 16 textViews and need to set something like this done:

for (int i=1; i<6; i++)
{
    int $RidInt = R.id.s; 
    tv[i] = (TextView)findViewById($RidI         


        
相关标签:
3条回答
  • 2020-12-01 15:35

    try this.

    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 讨论(0)
  • 2020-12-01 15:38

    I had to wrap mine in a try/catch wrapper, not sure why, but I'm Very New.

    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);}}
    
    0 讨论(0)
  • Something like that?

    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 讨论(0)
提交回复
热议问题