標題有點混亂,讓我進一步解釋。我試圖製作的一個應用程序具有多個按鈕,打開一個看起來相同的新活動,但文本根據點擊的按鈕而不同(如字典)。因此,我沒有重新創建活動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 _...」?