微调器只选择了一次nedded,Android Studio
我试图使用微调器只选择了一次nedded,Android Studio,android,Android,我试图使用工具栏中的微调器实现语言更改。现在,我为微调器提供了以下代码: initList(); Spinner spinnerCountries = findViewById(R.id.spinner_limba); mAdapter = new SpinnerAdapterTara(Harta.this, mCountryList); spinnerCountries.setAdapter(mAdapter); spinnerCountries.setOnItemSelect
工具栏中的微调器实现语言更改。现在,我为微调器提供了以下代码:
initList();
Spinner spinnerCountries = findViewById(R.id.spinner_limba);
mAdapter = new SpinnerAdapterTara(Harta.this, mCountryList);
spinnerCountries.setAdapter(mAdapter);
spinnerCountries.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TaraITEM clickedItem = (TaraITEM) parent.getItemAtPosition(position);
String clickedCountryName = clickedItem.getCountryName();
if (clickedCountryName.equals("FR")){
setLocale("fr");
recreate();
} else if (clickedCountryName.equals("EN")){
setLocale("en");
recreate();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
编辑:添加了initList()
private void initList(){
mCountryList=新的ArrayList();
McCountryList.add(新项目(“RO”,R.drawable.steag_RO));
mCountryList.add(新项目(“EN”,R.drawable.steag_EN));
mCountryList.add(新项目(“FR”,R.drawable.steag_FR));
}
使用静态
变量保存最后一个选择
,并在重新创建活动
之前检查该变量,该活动会给您带来类似onChangeListener
//Place inside Harta like global variable
static String lastCountryName = "";
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TaraITEM clickedItem = (TaraITEM) parent.getItemAtPosition(position);
String clickedCountryName = clickedItem.getCountryName();
// compare here before recreating
if(!clickedCountryName.equals(lastCountryName)) {
lastCountryName = clickedCountryName;
if (clickedCountryName.equals("FR")){
setLocale("fr");
recreate();
} else if (clickedCountryName.equals("EN")){
setLocale("en");
recreate();
}
}
}
//放入类似Harta的全局变量中
静态字符串lastCountryName=“”;
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
TaraITEM clickedItem=(TaraITEM)父项.getItemAtPosition(position);
字符串clickedCountryName=clickedItem.getCountryName();
//在重新创建之前在此进行比较
如果(!clickedCountryName.equals(lastCountryName)){
lastCountryName=单击的CountryName;
如果(单击CountryName.equals(“FR”)){
setLocale(“fr”);
重新创建();
}否则如果(单击CountryName.equals(“EN”)){
设置语言环境(“en”);
重新创建();
}
}
}
使用静态
变量保存最后一个选择
,并在重新创建活动
之前检查该变量,该活动会给您带来类似onChangeListener
//Place inside Harta like global variable
static String lastCountryName = "";
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TaraITEM clickedItem = (TaraITEM) parent.getItemAtPosition(position);
String clickedCountryName = clickedItem.getCountryName();
// compare here before recreating
if(!clickedCountryName.equals(lastCountryName)) {
lastCountryName = clickedCountryName;
if (clickedCountryName.equals("FR")){
setLocale("fr");
recreate();
} else if (clickedCountryName.equals("EN")){
setLocale("en");
recreate();
}
}
}
//放入类似Harta的全局变量中
静态字符串lastCountryName=“”;
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
TaraITEM clickedItem=(TaraITEM)父项.getItemAtPosition(position);
字符串clickedCountryName=clickedItem.getCountryName();
//在重新创建之前在此进行比较
如果(!clickedCountryName.equals(lastCountryName)){
lastCountryName=单击的CountryName;
如果(单击CountryName.equals(“FR”)){
setLocale(“fr”);
重新创建();
}否则如果(单击CountryName.equals(“EN”)){
设置语言环境(“en”);
重新创建();
}
}
}
这是否回答了您的问题?你可以看看这个答案。该答案/问题对我的情况没有帮助。当用户在设置一次区域设置后可以重置区域设置时?您可以添加initList()
实现吗?这是否回答了您的问题?你可以看看这个答案。这个答案/问题对我的情况没有帮助。当用户在设置一次区域设置后可以重置区域设置时,你能添加你的initList()
实现吗?效果很好。现在我需要找到为什么它没有切换到EN
,而只切换到FR
就像一个符咒。现在我需要找到为什么它没有切换到EN
,而只切换到FR
//Place inside Harta like global variable
static String lastCountryName = "";
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
TaraITEM clickedItem = (TaraITEM) parent.getItemAtPosition(position);
String clickedCountryName = clickedItem.getCountryName();
// compare here before recreating
if(!clickedCountryName.equals(lastCountryName)) {
lastCountryName = clickedCountryName;
if (clickedCountryName.equals("FR")){
setLocale("fr");
recreate();
} else if (clickedCountryName.equals("EN")){
setLocale("en");
recreate();
}
}
}