在运行时更改语言的按钮第一次无效

时间:2019-06-28 19:07:24

标签: java android

我正在尝试在LinearLayout的顶部创建两个按钮,以允许用户更改应用程序的语言。但是,当我第一次按下这些按钮中的任何一个时,它只会重新创建页面(通过recreate()方法),而不会更改语言。第二次会更改语言,但是如果我按其他按钮,将带我到另一个Intent,然后返回,它将把语言更改为原始值。仅在第三次之后,整个应用程序的语言才会更改。

我已经尝试了SO中提供的几乎所有内容。 更改了xml属性(请参见下文),更新为Locale()方法的未描述版本,将Button更改为ImageButto并包含onFocusChange。

Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    tools:context=".StartPage"
    android:background="#404040"
    android:weightSum="1">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:background="#404040">

        <ImageButton
            android:id="@+id/enT"
            android:layout_width="91dp"
            android:layout_height="73dp"
            android:background="@android:color/transparent"
            android:clickable="true"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:onClick="actionEN"
            android:tag="@drawable/enflag"
            android:textColor="#FFB33636" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="229dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/start"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#ffff" />

        <Button
            android:id="@+id/grT"
            android:layout_width="86dp"
            android:layout_height="59dp"
            android:adjustViewBounds="true"
            android:background="@android:color/transparent"
            android:clickable="true"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:onClick="actionGR"
            android:text="GR"
            android:textColor="#FFB33636" />
        />

    </LinearLayout>


    <View
        android:layout_width="match_parent"
        android:layout_height="7px"
        android:background="#FFB33636"/>

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:divider="#252525"
        android:dividerHeight="1dp"
        android:drawSelectorOnTop="false"
        android:focusableInTouchMode="false"
        android:fastScrollEnabled="false"
        android:headerDividersEnabled="false">

    </ListView>

</LinearLayout>

OnFocusChange

input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    v.performClick();
                }
            }
        });

当前代码

translateToEn = (ImageButton) findViewById(R.id.enT);
 public void actionEN(View view) {

        Locale locale = new Locale("en");
        Locale.setDefault(locale);

        Resources resources = getBaseContext().getResources();

        Configuration configuration = resources.getConfiguration();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }

        resources.updateConfiguration(configuration, resources.getDisplayMetrics());
        recreate();
    }

问题在于,我首先尝试的所有操作都会重新创建视图,只有第二次才翻译页面。

0 个答案:

没有答案