请帮助我是java和android的新手。
以下是字符串数组资源
的示例的strings.xml
<resources>
<string-array name="dance_steps">
<item>Step1</item>
<item>Step2</item>
<item>Step3</item>
<item>Step4</item>
</string-array>
</resources>
如何获取字符串并将其放在java代码中的不同TextView
上?
TextView step1, step2, step3, step4;
step1 = (TextView) findViewById(R.id.textview1);
step2 = (TextView) findViewById(R.id.textview2);
step3 = (TextView) findViewById(R.id.textview3);
step4 = (TextView) findViewById(R.id.textview4);
Resources res = getResources();
String[] steps = res.getStringArray(R.array.dance_steps);
step1.setText(item number 1 in String Array);
step2.setText(item number 2 in String Array);
step3.setText(item number 3 in String Array);
step4.setText(item number 4 in String Array);
三江源。
答案 0 :(得分:0)
step1.setText(steps[0]);
step2.setText(steps[1]);
step3.setText(steps[2]);
step4.setText(steps[3]);
你应该学习Java基础知识
答案 1 :(得分:0)
你只需要传递字符串数组。这是更高效的代码 -
Map<String, String> users = new HashMap<>();
// here when read every line from file, split the line with "-"
// and store it in the map
String[] strs = line.split("-");
users.put(strs[0], strs[1]);
// then when you check, you can do as below
if (users.containsKey(name)) {
System.out.println("Phone: "+users.get(name));
}
答案 2 :(得分:0)
TextView step[] = new TextView[4];
step[0] = (TextView) findViewById(R.id.textview1);
step[1] = (TextView) findViewById(R.id.textview2);
step[2] = (TextView) findViewById(R.id.textview3);
step[3] = (TextView) findViewById(R.id.textview4);
Resources res = getResources();
String[] steps = res.getStringArray(R.array.dance_steps);
for(int i = 0 ; i < steps.length ; i++){
step[i].setText(steps[i]);
}