2014-11-06 140 views
0

我正在構建一個應用程序,用戶可以在應用程序啓動時選擇想要設置的語言。彈出窗口將顯示「選擇您的語言」,其中一個用於阿拉伯語,另一個用於英語。從Alert for Android設置語言(XAMARIN Studio)

的問題是,當我改變了語言環境和執行這條線

SetContentView (Resource.Layout.Main); 

的應用打破了。沒有錯誤或任何異常,但所有控件都停止捕獲事件。我嘗試使用this和this.Class聲明一個Intent,當我調用StartActivity時,它就像重新啓動整個應用程序一樣,然後彈出窗口再次選擇語言。我是新來的Android開發爲我花了我近兩年來在SAP ABAP工作,所以我可能會問一個愚蠢的問題:d

這裏是我的代碼

using System; 

using Android.App; 
using Android.Content; 
using Android.Runtime; 
using Android.Views; 
using Android.Widget; 
using Android.OS; 

namespace Khums 
{ 
    [Activity (Label = "Khums", MainLauncher = true, Icon = "@drawable/icon")] 
    public class MainActivity : Activity 
    { 

     protected override void OnCreate (Bundle bundle) 
     { 
      base.OnCreate (bundle); 

      // Set our view from the "main" layout resource 
      int r = Resource.Layout.Main; 
      SetContentView (Resource.Layout.Main); 

      var languageIso = "ar-SA"; 

      AlertDialog.Builder alert = new AlertDialog.Builder (this); 
      //alert.SetTitle ("Selected Language"); 

      AlertDialog alertDiaog = alert.Create(); 
      alertDiaog.SetTitle ("Select Language:"); 

      alertDiaog.SetButton ("العربية", (s, EventArgs) => { 

       languageIso = "ar-SA"; 
       var locale = new Java.Util.Locale(languageIso); 
       Java.Util.Locale.Default = locale; 
       var config = new Android.Content.Res.Configuration{Locale = locale }; 
       BaseContext.Resources.UpdateConfiguration(config, BaseContext.Resources.DisplayMetrics); 
       //base.SetContentView(r); 
       //Intent intent = new Intent(this, this.Class); 
       //StartActivity(intent); 

       SetContentView (Resource.Layout.Main); 
      }); 

      alertDiaog.SetButton2 ("English", (s, EventArgs) => { 

       languageIso = "en-US"; 
       var locale = new Java.Util.Locale(languageIso); 
       Java.Util.Locale.Default = locale; 
       var config = new Android.Content.Res.Configuration{Locale = locale }; 
       BaseContext.Resources.UpdateConfiguration(config, BaseContext.Resources.DisplayMetrics); 
       SetContentView (Resource.Layout.Main); 
      }); 


      alertDiaog.Show(); 

      Button button = FindViewById<Button> (Resource.Id.myButton); 

      RadioButton rb_FirstTime = FindViewById<RadioButton> (Resource.Id.radioButton1); 
      RadioButton rb_Regular = FindViewById<RadioButton> (Resource.Id.radioButton2); 

      EditText ti_lyearBalance = FindViewById<EditText> (Resource.Id.ti_lastBalance); 
      EditText ti_Balance = FindViewById<EditText> (Resource.Id.ti_Balance); 
      EditText ti_Clothes = FindViewById<EditText> (Resource.Id.ti_Clothes); 
      EditText ti_Food = FindViewById<EditText> (Resource.Id.ti_Food); 
      EditText ti_Perfumes = FindViewById<EditText> (Resource.Id.ti_Perfumes); 
      EditText ti_Subscriptions = FindViewById<EditText> (Resource.Id.ti_Subscriptions); 
      EditText ti_Others = FindViewById<EditText> (Resource.Id.ti_Others); 

      TextView lbl_lyearBalance = FindViewById<TextView> (Resource.Id.lbl_lastBalance); 

      rb_FirstTime.Click += RadioButtonHandler; 
      rb_Regular.Click += RadioButtonHandler; 
      button.Click += MyButtoHandler; 
     } 

     private void RadioButtonHandler(object sender, EventArgs e) 
     { 

     } 

     private void MyButtoHandler(object sender, EventArgs e) 
     { 


     } 

     private double calculateKhumus (double[] amounts, Boolean isRegular) 
     { 


     } 

     private void LangSwitchHndler(Object sender, EventArgs e) 
     { 

     } 


    } 
} 

可以請你告訴我,我什麼在這裏做錯了。我試圖使用togglebutton而不是alert和標準按鈕,但最終出現同樣的問題。謝謝。

回答

0

它試了兩天後就解決了。我所做的只是創建一個菜單並將目標框架更改爲Android 3.1 Honeycomb。然後我剛開始活動,一切工作正常。

所以這個問題只是目標框架!