循环通过 editTexts 来检查值 [英] Looping through editTexts to check values

查看:20
本文介绍了循环通过 editTexts 来检查值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 100 个空白 EditText 的布局,所有这些都基于它们的行/列 ID(例如 box0101、box0102 等)命名.

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

然后我有另一个布局,其中有 100 个 TextView,布局完全相同,每个布局中有一个字母,使用相同的约定命名(answerbox0101、answerbox0102 等)

I then have another layout with 100 TextViews in exactly the same layout with one letter in each, named using the same convention (answerbox0101, answerbox0102 etc.)

我想编写一个循环,根据 answerbox0101 对 box0101 进行检查,依此类推,直到其中一个框不匹配,或者达到 100 并且所有框都匹配为止.

I want to write a loop that checks box0101 against answerbox0101, and so on until either one of the boxes does not match up, or it gets to 100 and all the boxes match.

我很擅长编写循环的逻辑,我的问题是我需要循环参数成为 EditText 名称的一部分!我怎样才能克服这个问题?

I am fine with writing the logic of the loop, my issue is that i need the looping parameter to be part of the name of the EditText! How can I overcome this?

推荐答案

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;
    } 
}

这篇关于循环通过 editTexts 来检查值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆