有没有办法在应用程序运行时更改语言

每个人。:) 我有一个具有多布局的应用程序,我正在尝试在运行该应用程序时更改语言。我发现这段代码可以帮助我更改语言


using Android.Content;

using Android.OS;


using Java.Util;


namespace RuntimeAppLanguage

{

    internal class LanguageManager

    {

        private const string MYLANGUAGE = "myLanguage";

        private const string MYPREF = "myPreference";


        public static Context LoadLanguage(Context context)

        {

            var loadedLanguage = GetLanguage(context, Locale.Default.Language);

            return ChangeLanguage(context, loadedLanguage);

        }


        public static Context ChangeLanguage(Context context, string language)

        {

            SaveLanguage(context, language);

            if (Build.VERSION.SdkInt >= BuildVersionCodes.N)

            {

                return ChangeForAPI24(context, language);

            }

            return ChangeForLegacy(context, language);

        }


        private static string GetLanguage(Context context, string Language)

        {

            var privatePreference = context.GetSharedPreferences(MYPREF, FileCreationMode.Private);

            return privatePreference.GetString(MYLANGUAGE, Language);

        }


        private static void SaveLanguage(Context context, string language)

        {

            var privatePreference = context.GetSharedPreferences(MYPREF, FileCreationMode.Private);

            var editor = privatePreference.Edit();

            editor.PutString(MYLANGUAGE, language);

            editor.Apply();

        }


        private static Context ChangeForAPI24(Context context, string language)

        {

            // for api >= 24

            var locale = new Locale(language);

            Locale.Default = locale;

            var configuration = context.Resources.Configuration;

            configuration.SetLocale(locale);

            configuration.SetLayoutDirection(locale);


            return context.CreateConfigurationContext(configuration);

        }


      

        }

    }

}

我试图让它在这种布局下工作,但我最终得到了循环:(


我想做的是当用户选中英语时,阿拉伯语开关变为取消选中等等。


慕盖茨4494581
浏览 92回答 1
1回答

牛魔王的故事

我用“Chinese”,“English”和“Thailand”写了一个简单的例子,是否满足你的需求:1.LanguageManager类,代码同上。2.&nbsp;MainActivity,包括一个TextView和一个Button;3.在BaseActivity中public class BaseActivity: AppCompatActivity{&nbsp; &nbsp; protected override void AttachBaseContext(Context @base)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; base.AttachBaseContext(LanguageManager.LoadLanguage(@base));&nbsp; &nbsp; }}4.在可以设置语言的SettingActivity中,axml和你的类似public class SettingActivity : BaseActivity, CompoundButton.IOnCheckedChangeListener{&nbsp; &nbsp; private Switch swCh;&nbsp; &nbsp; private Switch swEn;&nbsp; &nbsp; private Switch swTh;&nbsp; &nbsp; private Bundle s;&nbsp; &nbsp; protected override void OnCreate(Bundle savedInstanceState)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; base.OnCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; SetContentView(Resource.Layout.setting);&nbsp; &nbsp; &nbsp; &nbsp; // Create your application here&nbsp; &nbsp; &nbsp; &nbsp; initView();&nbsp; &nbsp; }&nbsp; &nbsp; private void initView()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Button back = FindViewById<Button>(Resource.Id.back);&nbsp; &nbsp; &nbsp; &nbsp; back.Click += delegate { Finish(); };&nbsp; &nbsp; &nbsp; &nbsp; swCh = FindViewById<Switch>(Resource.Id.switch1);&nbsp; &nbsp; &nbsp; &nbsp; swEn = FindViewById<Switch>(Resource.Id.switch2);&nbsp; &nbsp; &nbsp; &nbsp; swTh = FindViewById<Switch>(Resource.Id.switch3);&nbsp; &nbsp; &nbsp; &nbsp; var s = GetSharedPreferences("myPreference", FileCreationMode.Private).GetString( "myLanguage", Locale.Default.Language);&nbsp; &nbsp; &nbsp; &nbsp; switch (s)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "ch":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swCh.Checked = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "en":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swEn.Checked = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case "th":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swTh.Checked = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; swCh.SetOnCheckedChangeListener(this);&nbsp; &nbsp; &nbsp; &nbsp; swEn.SetOnCheckedChangeListener(this);&nbsp; &nbsp; &nbsp; &nbsp; swTh.SetOnCheckedChangeListener(this);&nbsp; &nbsp; }&nbsp; &nbsp; public void OnCheckedChanged(CompoundButton buttonView, bool isChecked)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (isChecked)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; switch (buttonView.Id)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case Resource.Id.switch1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swEn.Checked = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swTh.Checked = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LanguageManager.ChangeLanguage(this, "ch");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case Resource.Id.switch2:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swCh.Checked = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swTh.Checked = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LanguageManager.ChangeLanguage(this, "en");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case Resource.Id.switch3:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swEn.Checked = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swCh.Checked = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LanguageManager.ChangeLanguage(this, "th");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//restart application to change language&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent intent = new Intent(this, typeof(MainActivity));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; intent.SetFlags(ActivityFlags.ClearTask | ActivityFlags.NewTask);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StartActivity(intent);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}5.创建包含每种语言的values-en , values-thstringsa.值/字符串<string name="change_language">改变语言</string><string name="setting">设置</string><string name="chinese">中文</string><string name="english">英语</string><string name="thailand">泰语</string>b.values-en/字符串<string name="change_language">change language</string><string name="setting">setting</string><string name="chinese">chinese</string><string name="english">english</string><string name="thailand">thailand</string>c.values-th/字符串<string name="change_language">เปลี่ยนภาษา</string><string name="setting">เปลี่ย</string><string name="chinese">ชาวจีน</string><string name="english">อังกฤษ</string><string name="thailand">ประเทศไทย</string>ps: Text中的所有内容都要使用@string/***,并且每种语言在values/string像这样的效果:https://i.stack.imgur.com/JQl9X.gif
打开App,查看更多内容
随时随地看视频慕课网APP