我正在開發一個項目,在該項目中,我必須更改按鈕單擊的區域設置。我已經將默認語言環境設置爲印地語。當應用第一次啓動時,內容以印地文顯示。當我點擊按鈕時,它會變成英文。現在我想要的是,當我再次按下按鈕的內容改變回到印地語。但是這不起作用。我將van hindi轉換成英文,但反之亦然。下面我張貼我的代碼:單擊按鈕更改語言:Android
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
}
});
}
}
可能是你應該replce 否則,如果(LOCALE_ENGLISH.equals(mLocale)) 與 否則,如果(LOCALE_ENGLISH.equals(mLocale.toString())) – 2012-08-09 06:21:20
你好有解決這個問題? – Sultan 2014-12-22 07:03:20