我目前正在开发android 测验应用程序,我实现了“重置”按钮,可以重置问题并随机化它们,但是我' m在调用MainActivity.java中的.xml文件中的随机字符串时遇到了麻烦。Java/Xml/Android - 从Java中调用随机字符串String文件
我都在像这样的strings.xml文件中列出了我的问题,以使它们易于翻译:
<string name="q1">The reactor at the site of the Chernobyl nuclear disaster is now in which country?</string>
<string name="a1">A. Slovakia</string>
<string name="b1">B. Ukraine</string>
<string name="c1">C. Hungary</string>
<string name="d1">D. Russia</string>
<string name="q1answer">B</string>
所有的问题在该文件中列出像Q1, q2,q3,和ABCD答案相同:a1,b1,c1,d1; a2,b2,c2,d2等。
有很多问题,但按钮只选择其中5个并将它们显示在屏幕上。该问题是,我只是不能访问字符串,如果我想使用随机生成一个整数可能发现它们的.xml:
for (int i = 1; i <= 5; i++) {
// I managed to get the identifier of the TextView with the for loop like that (TextViews for questios are listed q1q, q2q, q3q, q4q, q5q):
// I would like to do the same thing down there with Strings.
TextView question = (TextView) findViewById(getResources().getIdentifier("q" + i + "q", "id", this.getPackageName()));
// randomQuestion is the number of the question in random number generator from different method.
randomQuestion = randomizeNumbers();
// And here I'm stuck, this will show "q1-randomNumber" instead of the real question,
// because it does not see it as an ID. I tried various different solutions, but nothing works.
question.setText("q" + randomQuestion);
// I left the most silly approach to show what I mean.
}
我能做些什么,以使计算机区分名的字符串?所以它显示的是真正的问题而不是“q1”“q6”“q17”?
感谢您提前帮忙!
可悲的是我被告知使用的.xml所以它是翻译之后轻松,但第一个回复解决问题 :)。 (); getIdentifier(“q”+ randomQuestion,“string”,this.getPackageName()); 完美的作品,早些时候我没有将“id”改为“string”。谢了哥们! – Qlak
很高兴帮助!试图给一点额外的东西:如果解决了问题,D可以自由地“回答”答案。祝你好运 ! –