2

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

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

3 回答 3

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
          }
     });
   }
 }
于 2012-08-09T06:17:43.390 回答
0

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

于 2012-09-03T16:59:36.233 回答
-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;
}
于 2017-07-09T15:34:38.527 回答