我有以下元素:
<EditText
android:id="@+id/kernelb11"
android:layout_width="@dimen/matrixBoxWidthHeight"
android:layout_height="@dimen/matrixBoxWidthHeight"
android:layout_below="@+id/textView1"
android:layout_margin="@dimen/marginOne"
android:background="@color/white1"
android:gravity="center"
android:inputType="numberSigned"
android:maxLength="2"
android:text="0" >
我正在尝试使用这样的代码检索它,但它返回的所有内容都是false
:
String name = "kernelb11"
int a = this.getResources().getIdentifier(name, "id", getPackageName());
CharSequence asa = this.getResources().getText(a); //Returning false
String as = (String) this.getResources().getText(a);
由于总体要求,我无法使用findViewById()
答案 0 :(得分:1)
int a = this.getResources().getIdentifier(name, "id", getPackageName());
此标识符指向R.id
资源,但Resources.getText(int)
需要R.string
资源标识符。
您首先必须按ID找到EditText
,然后获取其内容。像这样:
String name = "kernelb11"
int a = this.getResources().getIdentifier(name, "id", getPackageName());
EditText et = (EditText)findViewById(a);
CharSequence asa = ed.getText();
答案 1 :(得分:0)
尝试删除代码中的this.
String name = "kernelb11";
int layoutID = getResources().getIdentifier(name, "id", getPackageName());