Java 无法通过getIdentifier检索资源
我的XML中有以下元素Java 无法通过getIdentifier检索资源,java,android,Java,Android,我的XML中有以下元素 <EditText android:id="@+id/kernelb11" android:layout_width="@dimen/matrixBoxWidthHeight" android:layout_height="@dimen/matrixBoxWidthHeight" android:layout_below="@+id/textViewb" android:
<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>
但是在getIdentifier
中,只返回了“0”,有人能告诉我原因吗?试试这个:
int a = this.getResources().getIdentifier(name, "id", getPackageName());
正如其他人指出的那样,这不是最好的方法(尽管,应该是可行的)。通常,您应该使用
findViewById
。如果在name
参数中指定最后两个参数,则只能使用null
,如:
String name = "my.package.name:id/kernelb11";
如果您只想使用该名称,您需要这样做:
getIdentifier(name, "id", "my.package.name")
EditText et = (EditText)findViewById(R.id.kernellb11);
String as = et.getText().toString();
()
然而,这可能无法满足您的需要。要从EditText获取字符串,通常应执行以下操作:
getIdentifier(name, "id", "my.package.name")
EditText et = (EditText)findViewById(R.id.kernellb11);
String as = et.getText().toString();