在安卓运行时抓取复选框名称



我在单击按钮时循环浏览复选框列表。 我要做的是在运行时获取复选框的名称,以去除名称中指定的整数值。 我不想获取值或 id。 所以在字符串.xml文件,<string name="checkList1">Pain assessment.</string>,我试图在运行时获取 checkList1。 我可以毫无问题地获取文本。 目前,我正在使用以下代码循环浏览视图元素:

RelativeLayout root = (RelativeLayout)findViewById(R.id.Root);
            for (int i = 0; i < root.getChildCount(); i++)
            {
                View v1 = root.getChildAt(i);
                Class c = v1.getClass();
                if (c == CheckBox.class)
                {
                    CheckBox thisBox = (CheckBox)v1;
                    if (thisBox.isChecked())
                    {
                        String text = (String)thisBox.ge;
                        DoDailyCheckListUpdate(thisBox.isChecked(),checkBoxCount);
                        countItemsFinished++;
                    }
                    checkBoxCount++;
                }
            }

我正在寻找的是以某种方式获取复选框此框的名称。 因此,当它循环并点击"疼痛评估"复选框时,我希望能够拉出checkList1。 没有根据我找到的文本翻录字符串.xml文件来获取名称,我希望也许有一个我可能忽略的更简单的解决方案。

提前谢谢你。

CheckBoxTextView扩展而来,所以从中检索文本非常简单:

String text = thisBox.getText().toString();

http://developer.android.com/reference/android/widget/CheckBox.html

如果要检索字符串的键名。我建议你把它放到对象的标签中:

thisBox.setTag(getResources().getResourceEntryName(R.string. checkList1);

像那样检索它:

String text = (String)thisBox.getTag();

这应该可以解决问题。

最新更新