2015-02-12 90 views
0

标题有点混乱,让我进一步解释。我试图制作的一个应用程序具有多个按钮,打开一个看起来相同的新活动,但文本根据点击的按钮而不同(如字典)。因此,我没有重新创建活动50多次,而是使用新的Intent为第一个活动中的onClick()创建了一个新方法,并向第二个活动添加了getIntent()。是否可以将字符串值更改为R.string引用名称?

//first activity method 
public final static String TRANSFER_SYMBOL = "com.engineering.dictionary.MESSAGE"; 

public void openView(View view) 
{ 
    Intent open = new Intent(this,displayCharHB.class); 
    Button setBtn = (Button) findViewById(view.getId()); 
    String grabText = setBtn.getText().toString(); 
    String thisChar = "sym_hira_" + grabText; 
    open.putExtra(TRANSFER_SYMBOL,thisChar); 
    startActivity(open); 
} 


//second Activity method 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    Intent grabChar = getIntent(); 
    String thisChar = grabChar.getStringExtra(hira_basic.TRANSFER_SYMBOL); 
    //String strValue = "R.string." + thisChar; 


    TextView displaySym = (TextView) findViewById(R.id.charDisplay); 
    displaySym.setText(thisChar); 
} 

sym_hira_ +无论是存储在grabText是在一个strings.xml中字符串的名称(例如,grabText = “RO”,sym_hira_ro是一个字符串名称)。是否有可能让它引用该字符串名称与setText(),而不是实际将文本设置为“sym_hira _...”?

回答

1

是否有可能得到它的引用字符串名称用的setText()

使用getIdentifier使用的名字从strings.xml获得价值:

int resId = getResources().getIdentifier(thisChar, "string",getPackageName()); 
displaySym.setText(getResources().getString(resId)); 
0
你可以使用BeanShell的执行

“R.string.xxxx”命令获取字符串资源ID。

这是我的代码示例;

String str = ""; 
Interpreter i = new Interpreter(); 
i.set("context", MainActivity.this); 
Object res = i.eval("com.example.testucmbilebase.R.string.hello_world"); 
if(res != null) { 
    Integer resI = (Integer)res; 
    str = MainActivity.this.getResources().getString(resI); 
} 
相关问题