2015-02-12 95 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); 
} 
相關問題