单击按钮更改语言:Android

时间:2012-08-09 06:12:45

标签: android localization

我正在开发一个项目,我必须在按钮点击时更改区域设置。我已将默认语言环境设置为hindi。应用程序首次启动时,内容以hindi显示。当我点击按钮时,它将变为英语。现在我希望当我再次按下按钮时,内容会改变回印度语。但这不起作用。我把印地文切换到英文,但反之亦然。下面我发布我的代码:

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
        }
    });
}
}

3 个答案:

答案 0 :(得分:3)

更改else if阻止

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.toString()))
              {
                  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
          }
     });
   }
 }

答案 1 :(得分:0)

我做的不是正确的方法。每当我调用setContentView()时,我之前的视图都会失效。因此,为了使按钮工作,需要再次使用按钮id,并且需要在按钮上设置onClickListener。这将造成混乱,阻止内部块等等。

答案 2 :(得分:-1)

试试这个方法:

  public String languageCheck() {
    String lCode="en";
    if(PersistData.getStringData(getApplicationContext(), "LNG").equalsIgnoreCase("en")) {
        lCode="hi";
    }
    else if(PersistData.getStringData(getApplicationContext(), "LNG").equalsIgnoreCase("hi")) {
        lCode="en";
    }
    return lCode;
}