我想做一个简单的本地化演示,我有一个微调器来选择语言和文本视图来显示所需语言的字符串,这是我的代码
public class MainActivity extends Activity implements OnItemSelectedListener
{
Spinner laguageSpinner;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
laguageSpinner = laguageSpinner = ((Spinner) findViewById(R.id.spinner1));
laguageSpinner.setOnItemSelectedListener(this);
generateLanguageItems();
}
public void generateLanguageItems()
{
laguageSpinner = laguageSpinner = ((Spinner) findViewById(R.id.spinner1));
List<String> list = new ArrayList<String>();
list.add("English");
list.add("عربي");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
R.layout.el_spinner_txtview, list);
dataAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
laguageSpinner.setAdapter(dataAdapter);
}
@Override
public void onItemSelected(AdapterView<?> adapter, View arg1, int pos,
long arg3)
{
Toast.makeText(this, adapter.getItemAtPosition(pos) + "",
2000).show();
switch(pos)
{case 0: changeLocale( "en" );break;
case 1: changeLocale( "ar" );break;}
}
public void changeLocale( String code )
{
Locale locale = new Locale(code);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
this.setContentView(R.layout.activity_main);
generateLanguageItems();
}
}
本地化更改代码完美运行,我通过更改此
进行了测试 switch(pos)
{case 0: changeLocale( "en" );break;
case 1: changeLocale( "ar" );break;}
到那个
switch(pos)
{case 0: changeLocale( "ar" );break;
case 1: changeLocale( "en" );break;}
我的问题是旋转器项目在运行时不再生成我有一个空的微调器,试图对代码进行一些更改但没有运气,你能帮助我吗?
--- --- EDIT 事实上我试过这个
public void generateLanguageItems()
{
....
laguageSpinner = ((Spinner) findViewById(R.id.spinner1));
laguageSpinner.setAdapter(dataAdapter);
}
这种方式可以生成微调器项,但onItemSelected()
不再触发,
然后我添加了
public void generateLanguageItems()
{
...
laguageSpinner.setOnItemSelectedListener(null);
laguageSpinner = ((Spinner) findViewById(R.id.spinner1));
laguageSpinner.setAdapter(dataAdapter);
laguageSpinner.setOnItemSelectedListener(this);
}
现在它以递归方式触发,我无法点击微调器并为其项目充气!
答案 0 :(得分:0)
为什么你有这个双重任务:
laguageSpinner = laguageSpinner = ((Spinner) findViewById(R.id.spinner1));
将其更改为
laguageSpinner = (Spinner) findViewById(R.id.spinner1);
再试一次。
答案 1 :(得分:0)
我添加了一个破坏递归的条件,这是工作代码
public class MainActivity extends Activity implements OnItemSelectedListener
{
Spinner laguageSpinner;
int currentLocale=0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
laguageSpinner = ((Spinner) findViewById(R.id.spinner1));
laguageSpinner.setOnItemSelectedListener(this);
generateLanguageItems(0);
}
public void generateLanguageItems(int pos)
{
List<String> list = new ArrayList<String>();
list.add("English");
list.add("عربي");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
R.layout.el_spinner_txtview, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
laguageSpinner.setOnItemSelectedListener(null);
laguageSpinner = ((Spinner) findViewById(R.id.spinner1));
laguageSpinner.setAdapter(dataAdapter);
laguageSpinner.setOnItemSelectedListener(this);
laguageSpinner.setSelection(pos);
currentLocale = pos;
}
@Override
public void onItemSelected(AdapterView<?> adapter, View arg1, int pos, long arg3)
{ changeLocale( pos ); }
@Override
public void onNothingSelected(AdapterView<?> arg0)
{ }
public void changeLocale( int pos )
{
if ( currentLocale == pos)
return;
Locale locale;
if (pos == 0)
locale = new Locale("en");
else
locale = new Locale("ar");
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
this.setContentView(R.layout.activity_main);
generateLanguageItems(pos);
}
}