我的 XML 中有以下元素
<EditText
android:id="@+id/kernelb11"
android:layout_width="@dimen/matrixBoxWidthHeight"
android:layout_height="@dimen/matrixBoxWidthHeight"
android:layout_below="@+id/textViewb"
android:layout_margin="@dimen/marginOne"
android:background="@color/white1"
android:gravity="center"
android:inputType="numberSigned"
android:maxLength="2"
android:text="5" >
<requestFocus />
</EditText>
我正在尝试在代码中检索它的值,例如:
String name = "kernelb11";
int a = this.getResources().getIdentifier(name, null, null); //
String as = (String) this.getResources().getText(a);
但是在 getIdentifier
处只返回“0”,谁能告诉我为什么?
最佳答案
试试这个:
int a = this.getResources().getIdentifier(name, "id", getPackageName());
正如其他人指出的那样,这不是最好的方法(尽管应该可行)。
通常,您应该使用 findViewById
来代替。
关于java - 无法通过 getIdentifier 检索资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19800156/