0

我的 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'谁能告诉我为什么?

4

2 回答 2

1

试试这个:

int a = this.getResources().getIdentifier(name, "id", getPackageName());

正如其他人指出的那样,这不是最好的方法(尽管应该可行)。
通常,您应该findViewById改用。

于 2013-11-05T22:28:41.297 回答
1

null如果在参数中指定它们,则只能使用最后两个参数name,例如:

String name = "my.package.name:id/kernelb11";

如果您只想使用名称,则需要这样做:

getIdentifier(name, "id", "my.package.name")

文档


但是,这可能不会做你想要的。要从 EditText 中获取字符串,您通常应该执行以下操作:

EditText et = (EditText)findViewById(R.id.kernellb11);
String as = et.getText().toString();
于 2013-11-05T22:28:54.247 回答