所以这里我有示例代码:

TextView aDisplayw=(TextView)findViewById(R.id.display);

protected void set(String resource) {
   String toResource = "R.string" + resource;//How to convert it into resource?
   aDisplay.setText(getResources().getText(forString));
}


那么有没有办法做到这一点?

最佳答案

使用Resources.getIdentifier

int resourceId = getResources().getIdentifier(
    resource, // Name
    "string", // Type
    getPackageName());
aDisplay.setText(resourceId);

10-07 16:09