更改Lang错误,Button不起作用Android Studio

时间:2018-11-03 00:27:02

标签: java android

我的代码有一个小问题,我创建了一个按钮来使我的应用程序更改语言。

public class ChangeLang extends AppCompatActivity {

FragmentActivity activity;
CallbackManager callbackManager;
Button changelanguage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    loadLocale();
    activity = this;


    //set change language button
    changelanguage = findViewById(R.id.change_lang_btn2);
    changelanguage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // Array of languages to dispaly in AlertDialog
            final String[] listitems ={"English","Spanish | Español","Portuguese | Português","French | français"};

            // Create AlertDialog to display list of languages
            AlertDialog.Builder alert = new AlertDialog.Builder(ChangeLang.this);
            alert.setTitle("Choose Language..");
            alert.setSingleChoiceItems(listitems, -1, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    if (i == 0){
                        //set English
                        setLocale("en");
                        recreate();
                    }
                   else if (i == 1){
                        //set Spanish
                        setLocale("es");
                        recreate();
                    }
                   else if (i == 2){
                        //set Portuguese
                        setLocale("pt");
                        recreate();
                    }
                    else if (i == 3){
                        //set french
                        setLocale("fr");
                        recreate();
                    }

                    //dismiss alert dialog when language is selected
                    dialogInterface.dismiss();
                }
            });
            alert.show();
        }
    });
}

private void setLocale(String lang) {
    Locale locale = new Locale(lang);
    Locale.setDefault(locale);
    Configuration configuration = new Configuration();
    configuration.locale = locale;
    getBaseContext().getResources().updateConfiguration(configuration,getBaseContext().getResources().getDisplayMetrics());

    //save data to sharedpreferences
    SharedPreferences.Editor editor = getSharedPreferences("lang_settings",MODE_PRIVATE).edit();
    editor.putString("My_lang",lang);
    editor.apply();
}

//load language saved in shared preferences
public void loadLocale(){
    SharedPreferences prefs = getSharedPreferences("lang_settings", Activity.MODE_PRIVATE);
    String language = prefs.getString("My_lang", "");
    setLocale(language);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

sdfdsf

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:gravity="center"
    tools:context="com.riipo.gt.MainActivity">


    <android.support.v7.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

    <FrameLayout
        android:id="@+id/drawer_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar">

        <fragment
            android:id="@+id/content"
            class="com.riipo.helpers.MyPreferenceFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </FrameLayout>
    <Button
        android:id="@+id/change_lang_btn2"
        android:layout_width="wrap_content"
        android:layout_height="45dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:backgroundTint="@color/accent"
        android:gravity="center_vertical"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:text="@string/change_language_button"
        android:textColor="@color/colorWhite"
        android:textSize="14sp"
        />
</RelativeLayout>

代码的想法是将区域称为br,en,es,fr。在我的应用程序首页SplashScreenActivity中,它运行正常。

如果您需要更多详细信息,请告诉我,谢谢您的耐心和帮助。

0 个答案:

没有答案