我正在尝试创建一个多语言应用,但我的主页是一个由图像和textViews组成的gridView。当我用Java创建一个列表时(如下所示):
String textoList[] = {"Visto", "Alojamento", "C.P.F.", "Alimentação", "Carteirinha", "Guia", "Registro", "Informações", "Assistência", "Biblioteca", "Calendário", "Transporte", "INT", "Intérprete", "F.A.Q."};
效果很好。但我意识到我不应该在Java中创建一个列表,而是使用已经在strings.xml文件中创建的数组,以便我的应用程序可以被翻译。基本上,我想要这个数组的文本。我怎么能这样做?
有人可以帮我一把吗?如果有人想测试它,完整的代码是:
package br.com.unbparaestrangeiros.unbparaestrangeiros;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
public class MainActivity extends Activity{
GridView gridView;
Resources r = getResources();
String[] textoList = r.getStringArray(R.array.itens);
//String textoList[] = {"Visto", "Alojamento", "C.P.F.", "Alimentação", "Carteirinha", "Guia", "Registro", "Informações", "Assistência", "Biblioteca", "Calendário", "Transporte", "INT", "Intérprete", "F.A.Q."};
int textoImagem[] = {R.drawable.visto, R.drawable.alojamento, R.drawable.cpf, R.drawable.alimentacao, R.drawable.carteirinha, R.drawable.guia, R.drawable.registro, R.drawable.informacoes, R.drawable.assistencia, R.drawable.biblioteca, R.drawable.calendario, R.drawable.transporte, R.drawable.informacoes, R.drawable.interprete, R.drawable.perguntas};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inicio);
gridView = (GridView) findViewById(R.id.gridView);
GridAdapter adapter = new GridAdapter(MainActivity.this, textoImagem, textoList);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
Intent case0 = new Intent(MainActivity.this, Visto.class);
startActivity(case0);
break;
case 1:
Intent case1 = new Intent(MainActivity.this, Alojamento.class);
startActivity(case1);
break;
case 2:
Intent case2 = new Intent(MainActivity.this, CPF.class);
startActivity(case2);
break;
case 3:
Intent case3 = new Intent(MainActivity.this, Alimentacao.class);
startActivity(case3);
break;
case 4:
Intent case4 = new Intent(MainActivity.this, Carteirinha.class);
startActivity(case4);
break;
case 5:
Intent case5 = new Intent(MainActivity.this, Guia.class);
startActivity(case5);
break;
case 6:
Intent case6 = new Intent(MainActivity.this, Registro.class);
startActivity(case6);
break;
case 7:
Intent case7 = new Intent(MainActivity.this, Informacoes.class);
startActivity(case7);
break;
case 8:
Intent case8 = new Intent(MainActivity.this, Assistencia.class);
startActivity(case8);
break;
case 9:
Intent case9 = new Intent(MainActivity.this, Biblioteca.class);
startActivity(case9);
break;
case 10:
Intent case10 = new Intent(MainActivity.this, Calendario.class);
startActivity(case10);
break;
case 11:
Intent case11 = new Intent(MainActivity.this, Transporte.class);
startActivity(case11);
break;
case 12:
Intent case12 = new Intent(MainActivity.this, Assessoria.class);
startActivity(case12);
break;
case 13:
Intent case13 = new Intent(MainActivity.this, Interprete.class);
startActivity(case13);
break;
case 14:
Intent case14 = new Intent(MainActivity.this, Perguntas.class);
startActivity(case14);
break;
}
}
});
}
}
答案 0 :(得分:1)
试试这个:
String[] textoList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inicio);
textoList = getResources().getStringArray(R.array.itens);
}
itens
中的strings.xml
数组如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
...............
......................
<string-array name="itens">
<item>Visto</item>
<item>Alojamento</item>
<item>C.P.F.</item>
<item>Alimentação</item>
<item>Carteirinha</item>
<item>Guia</item>
<item>Registro</item>
................
.......................
</string-array>
</resources>
希望这会有所帮助〜
答案 1 :(得分:1)
在array.xml
中创建一个名为app/src/main/res/values
的新文件。您可以像这样添加字符串:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="itens">
<item>Visto</item>
<item>Alojamento</item>
<!--Add your other words here-->
</string-array>
</resources>
您可以使用getResources().getStringArray(R.array.itens);
中的Activity
来访问它们。