gpt4 book ai didi

java - gridView - strings.xml 中的数组

转载 作者:搜寻专家 更新时间:2023-11-01 09:35:31 24 4
gpt4 key购买 nike

我正在尝试创建一个多语言应用程序,但我的主页是由图像和 TextView 组成的 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;

}
}
});
}

}

最佳答案

如果 app/src/main/res/values 中还没有名为 array.xml 的文件,请创建一个新文件。您可以像这样添加字符串:

<?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 访问它们。

关于java - gridView - strings.xml 中的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43574917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com