Looping through editTexts to check values

前端 未结 2 1071
悲哀的现实
悲哀的现实 2021-01-26 01:41

I have one layout with 100 blank EditTexts, all named based on their row / column IDs (e.g. box0101, box0102 etc.).

I then have another layout with 100 TextViews in exac

相关标签:
2条回答
  • 2021-01-26 02:03

    You can use Tag Property of View to set String Property, like if you are creating 100 editTexts, you can set tags of edit texts, like editText0101, editText0102, ... and same in answer editTexts answerbox0101, answerbox0102.... by this method you can get direct references of editText by tag name, by findViewByTag() method.

    0 讨论(0)
  • 2021-01-26 02:11
    for(int i=0;i<ROW_COUNT;i++){
        for(j=0;j<COLUMN_COUNT;j++){
            int editTextId=getResId("box"+i+j,this,id.class);
            int textViewId=getResId("answerbox"+i+j,this,id.class);
    
            EditText et=(EditText)findViewById(editTextId);
            TextView tv=(TextView)findViewById(textViewId);
    
           //Then do your comparison as you like and do the rest. 
        }   
    }
    
    public static int getResId(String variableName, Context context, Class<?> c) {
    
        try {
            Field idField = c.getDeclaredField(variableName);
            return idField.getInt(idField);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        } 
    }
    
    0 讨论(0)
提交回复
热议问题