นี่คือข้อเสนอแนะ: คุณสามารถรับID(ระบุเช่นโดยandroid:id="@+id/..My Str..) ซึ่งสร้างขึ้นRโดยใช้ชื่อที่ให้ไว้ (เช่นMy Str) ข้อมูลโค้ดโดยใช้getIdentifier()วิธีการนั้นจะเป็น:
public int getIdAssignedByR(Context pContext, String pIdString)
{
// Get the Context's Resources and Package Name
Resources resources = pContext.getResources();
String packageName = pContext.getPackageName();
// Determine the result and return it
int result = resources.getIdentifier(pIdString, "id", packageName);
return result;
}
จากภายใน Activityตัวอย่างการใช้งานควบคู่ไปกับfindViewByIdจะเป็น:
// Get the View (e.g. a TextView) which has the Layout ID of "UserInput"
int rID = getIdAssignedByR(this, "UserInput")
TextView userTextView = (TextView) findViewById(rID);