我正在开发一个项目,我必须在单击按钮时更改语言环境。我已将默认语言环境设置为印地语。当应用程序首次启动时,内容以印地语显示。当我点击按钮时,它会变成英文。现在我希望当我再次按下按钮时内容会变回印地语。但这不起作用。我将印地语切换为英语,反之亦然。下面我发布我的代码:
public class MainActivity extends Activity
{
Button btn1;
TextView textView1;
String LOCALE_HINDI = "hi";
String LOCALE_ENGLISH = "en";
Locale mLocale;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mLocale = new Locale(LOCALE_HINDI);
Locale.setDefault(mLocale);
Configuration config = new Configuration();
config.locale = mLocale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
this.setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
textView1 = (TextView)findViewById(R.id.textView1);
btn1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if(LOCALE_HINDI.equals(mLocale.toString()))
{
mLocale = new Locale(LOCALE_ENGLISH);
Locale.setDefault(mLocale);
Configuration config = new Configuration();
config.locale = mLocale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
MainActivity.this.setContentView(R.layout.activity_main);
Log.d("LocaleTest", "if block");
}//if
else if (LOCALE_ENGLISH.equals(mLocale))
{
mLocale = new Locale(LOCALE_HINDI);
Locale.setDefault(mLocale);
Configuration config = new Configuration();
config.locale = mLocale;
getBaseContext().getResources().updateConfiguration(config,
getBaseContext().getResources().getDisplayMetrics());
MainActivity.this.setContentView(R.layout.activity_main);
Log.d("LocaleTest", "else if block");
}//else if
}
});
}
}