【问题标题】:Android Change language in all activitiesAndroid 在所有活动中更改语言
【发布时间】:2020-07-06 18:00:27
【问题描述】:

我知道以前有人问过这样的问题,但他们帮不了我。 我正在创建一个简单的应用程序,我希望应用程序从英语转换为乌尔都语,因为我可以更改第一个活动语言但不知道如何在第二个活动中更改语言(整个应用程序) .在第二个活动中,我有一个 textView,当我通过按钮从第一个活动更改语言以选择我想要的语言时,我也想更改它。我已经在字符串中创建了资源。

这是我的代码

XML

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/find_Mazdoor"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.2" />

    <Button
        android:id="@+id/button5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/mazdoor_register"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button4"
        app:layout_constraintVertical_bias="0.04" />

    <Button
        android:id="@+id/language"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/change_language"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button5"
        app:layout_constraintVertical_bias="0.204" />
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity

public class MainActivity extends AppCompatActivity {

    Button language_btn, find_mazdoor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadLocale();
        setContentView(R.layout.activity_main);

        ActionBar actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setTitle(getResources().getString(R.string.app_name));

        find_mazdoor = findViewById(R.id.button4);
        find_mazdoor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,MainActivity2.class);
                startActivity(intent);
            }
        });

        language_btn = findViewById(R.id.language);
        language_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                changeLanguage();
            }
        });
    }

    private void changeLanguage() {

        final String[] language = {"English", "اردو"};

        AlertDialog.Builder mBuilder = new AlertDialog.Builder(MainActivity.this);
        mBuilder.setTitle("Change Language...");
        mBuilder.setSingleChoiceItems(language, -1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if (which == 0) {

                    //English
                    setLocale("en");
                    recreate();
                } else if (which == 1) {

                    //Urdu
                    setLocale("ur");
                    recreate();
                }

                dialog.dismiss();
            }
        });

        AlertDialog mDialog = mBuilder.create();
        mDialog.show();
    }

    private void setLocale(String lang) {

        Locale locale = new Locale(lang);
        Locale.setDefault(locale);

        Configuration config = new Configuration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            config.setLocale(locale);
        }
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());

        SharedPreferences.Editor editor = getSharedPreferences("Settings", MODE_PRIVATE).edit();
        editor.putString("My_Lang", lang);
        editor.apply();
    }

    public void loadLocale() {

        SharedPreferences prefs = getSharedPreferences("Settings", Activity.MODE_PRIVATE);
        String language = prefs.getString("My_Lang", "");
        setLocale(language);
    }
}

【问题讨论】:

    标签: android


    【解决方案1】:

    在 res 中创建文件夹 value-ur 然后在其中创建 string.xml

        String language = Locale.getDefault().getLanguage();
        if (language.equals("en")){
            setAppLocale("en");
    
        }else if(language.equals("ur")){
            setAppLocale("ur");
    
        }
        private void setAppLocale(String localeCode){
        Resources resources = getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        Configuration config = resources.getConfiguration();
        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.JELLY_BEAN_MR1){
            config.setLocale(new Locale(localeCode.toLowerCase()));
        } else {
            config.locale = new Locale(localeCode.toLowerCase());
        }
        resources.updateConfiguration(config, dm);
        }
    

    【讨论】:

    • 它正在改变,但是当我们回来再做一次时,它又变成了英语。
    • 使用上面的代码将语言环境保存在SharedPreferences
    • 好的,您必须将其保存在 sharedPrefrences 或 hawk 中,并在运行应用程序时在应用程序类中检查它
    猜你喜欢
    • 2018-05-15
    • 2015-07-15
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 2019-07-08
    • 2018-04-12
    • 2020-04-06
    • 1970-01-01
    相关资源
    最近更新 更多