TextInputLayout支持根据移动设备语言在android中的不同语言

时间:2016-11-24 11:29:05

标签: android xml android-edittext

我尝试根据移动设备选择的语言更改应用语言。 TextInputLayout正在使用英语,但不使用阿拉伯语(“ar”)语言。我没有在模拟器上显示编辑文本。我的布局如下:

<RelativeLayout
                android:id="@+id/rlCountrycode"
                android:layout_centerInParent="true"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">               

            <android.support.design.widget.TextInputLayout
                android:layout_toRightOf="@+id/country_picker_button"
                android:theme="@style/TextLabel"
                android:id="@+id/input_layout_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <EditText
                    android:textColorHint="#FFFFFF"
                    android:inputType="number"
                    android:id="@+id/phone"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:singleLine="true"
                    android:hint="@string/enter_phoneno" />


            </android.support.design.widget.TextInputLayout>

            </RelativeLayout>

和我的英文strings.xml文件如下:

<resources>

    <string name="enter_phoneno">Enter Mobile Number</string>
</resources>

我的strings.xml阿拉伯语如下:

<resources>

        <string name="enter_phoneno">أدخل رقم الجوال</string>
    </resources>

我的主要活动onCreate()方法如下:

EditText phoneno;
protected void onCreate(Bundle savedInstanceState) {
        // setTheme(R.style.Theme_Dialog);
        super.onCreate(savedInstanceState);
        String language = Locale.getDefault().getLanguage();
        String languageToLoad  = language; // your language
        Locale locale = new Locale(languageToLoad);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());

        setContentView(R.layout.activity_country_code);
phoneno = (EditText) findViewById(R.id.phone);

请帮帮我。提前谢谢。

1 个答案:

答案 0 :(得分:0)

 AlertDialog.Builder builder = new AlertDialog.Builder(DashboardActivity.this);
            builder.setTitle(R.string.languages);
            // Add the buttons
            builder.setPositiveButton(R.string.english, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    String languageToLoad = "en"; // your language
                    Locale locale = new Locale(languageToLoad);
                    Locale.setDefault(locale);
                    Configuration config = new Configuration();
                    config.locale = locale;
                    getBaseContext().getResources().updateConfiguration(config,
                            getBaseContext().getResources().getDisplayMetrics());
                    dialog.dismiss();
                    rEditor.putString("language", languageToLoad);
                    rEditor.commit();



                    Intent refresh = new Intent(DashboardActivity.this, DashboardActivity.class);
                    startActivity(refresh);
                    finish();

                }
            });
            builder.setNegativeButton(R.string.gujarati, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog

                    String languageToLoad = "gu"; // your language
                    Locale locale = new Locale(languageToLoad);
                    Locale.setDefault(locale);
                    Configuration config = new Configuration();
                    config.locale = locale;
                    getBaseContext().getResources().updateConfiguration(config,
                            getBaseContext().getResources().getDisplayMetrics());
                    dialog.dismiss();
                    rEditor.putString("language", languageToLoad);
                    rEditor.commit();


                    Intent refresh = new Intent(DashboardActivity.this, DashboardActivity.class);
                    startActivity(refresh);
                    finish();

                }
            });

            builder.create().show();

您必须重新加载活动以显示新语言文本意味着重新启动。