【发布时间】:2018-09-10 23:22:03
【问题描述】:
我正在使用 android studio,我正在尝试通过其变量名输出特定的字符串。基本上我有一个测验,根据用户的答案有不同的结果。
在我的 java 代码中
private void displayFinal(String stringVariable) {
TextView quantityTextView = (TextView) findViewById(R.id.choice);
String outPut = "some text here \n";
outPut += stringVariable+"\n";
outPut += getString(R.string.stringVariable);
outPut += "\n more text here";
quantityTextView.setText(outPut);
}
这里的 stringVariable 是一个字符串,它根据测验答案在 12 个值中的 1 个之间变化。所有 12 个都在我的 android studio 中的 resources.strings 文件中定义。
没有中间部分,代码可以正常工作,但是当我尝试通过变量调用字符串名称时,它不起作用。
输入一个特定的字符串名称,它工作得很好。
有没有什么方法可以在没有大量嵌套 if/else 语句的情况下将变量放在那里?
【问题讨论】:
标签: java android string variables