2012-08-09 151 views
2

我正在開發一個項目,在該項目中,我必須更改按鈕單擊的區域設置。我已經將默認語言環境設置爲印地語。當應用第一次啓動時,內容以印地文顯示。當我點擊按鈕時,它會變成英文。現在我想要的是,當我再次按下按鈕的內容改變回到印地語。但是這不起作用。我將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 
     } 
    }); 
} 
} 
+2

可能是你應該replce 否則,如果(LOCALE_ENGLISH.equals(mLocale)) 與 否則,如果(LOCALE_ENGLISH.equals(mLocale.toString())) – 2012-08-09 06:21:20

+0

你好有解決這個問題? – Sultan 2014-12-22 07:03:20

回答

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

你能否建議我該如何檢查病情? – Nitish 2012-08-09 06:37:12

+0

你正在比較字符串值與語言環境對象.. – 2012-08-09 06:38:14

+0

只是用這段代碼替換你的else-if塊並檢查它是否工作 – 2012-08-09 06:39:22

0

我在做什麼不正確的方法。每當我調用setContentView()時,我以前的視圖都會失效。所以,爲了使按鈕工作,再次需要按鈕id,onClickListener需要設置在按鈕上。這會造成混亂,塊內阻塞等。

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