代码之家  ›  专栏  ›  技术社区  ›  manick k

在android中使用SharedReference无法实现多语言支持

  •  1
  • manick k  · 技术社区  · 7 年前

    我正在努力实现 多语言支持 在我的申请中。

    我的应用程序包含两个活动,即 主要活动。班 下一个触觉。班 .

    当用户从中选择任何语言选项时 主要活动。班 ,我将在中保存所选语言 sharedpreference 并获取 共享引用 语言,以更改语言类型。但无法更改语言类型。

    有谁能帮我解决这个问题吗?谢谢你宝贵的时间!。。

    串。xml(值fr)

    <resources>
    <string name="app_name">myAPP</string>
    <string name="action_settings">Settings</string>
    <string name="hello">Bonjour !..</string>
    </resources>
    

    串。xml(值hi)

    <resources>
    <string name="app_name">myAPP</string>
    <string name="action_settings">Settings</string>
    <string name="hello">नमस्ते !..</string>
    </resources>
    

    主要活动。Java语言

    public class MainActivity extends AppCompatActivity {
    
    CheckBox chk_english, chk_french, chk_hindi;
    Button btn_next;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
    
        chk_french = (CheckBox) findViewById(R.id.chk_french);
        chk_hindi = (CheckBox) findViewById(R.id.chk_hindi);
        btn_next = (Button) findViewById(R.id.btn_next);
    
        chk_french.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    
                if (compoundButton.isChecked()) {
                    chk_hindi.setChecked(false);
                }
    
                saveLanguage("fr");
    
            }
        });
    
    
        chk_hindi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
    
                if (compoundButton.isChecked()) {
                    chk_french.setChecked(false);
                }
                saveLanguage("hi");
            }
        });
    
    
        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, NextActivity.class));
            }
        });
    
    
    }
    
    
    public void saveLanguage(String type) {
    
        SharedPreferences.Editor editor = getSharedPreferences("MY_LANGUAGE", MODE_PRIVATE).edit();
        editor.putString("myLanguage", type);
        editor.apply();
        editor.commit();
    
    }}
    

    下一个触觉。Java语言

    public class NextActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.next_layout);
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    
        loadLanguage();
    
    }
    
    public void loadLanguage() {
    
        SharedPreferences prefs = getSharedPreferences("MY_LANGUAGE", MODE_PRIVATE);
        String myLang = prefs.getString("myLanguage", "");
    
        if (myLang.equals("fr")) {
            setLanguage("fr");
        } else if (myLang.equals("hi")) {
            setLanguage("hi");
        } else if (myLang.equals("en")) {
            setLanguage("en");
        }
    
    }
    
    void setLanguage(String lang) {
    
        Locale locale = new Locale(lang);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
        this.setContentView(R.layout.next_layout);
    
    }}
    

    activity\u main。xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout            
    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="test.org.myapp.MainActivity">
    
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp">
    
    
    
        <CheckBox
            android:id="@+id/chk_french"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="French"
            android:paddingLeft="10dp"
            android:layout_marginBottom="30dp"/>
    
    
        <CheckBox
            android:id="@+id/chk_hindi"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hindi"
            android:paddingLeft="10dp"
            android:layout_marginBottom="30dp"
            />
    
        <Button
            android:id="@+id/btn_next"
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:text="Next"
            android:background="@color/colorPrimary"
            android:textColor="@android:color/white"
            />
    
    
    </LinearLayout>
    
    </android.support.design.widget.CoordinatorLayout>
    

    next\u布局。xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
    android:gravity="center"
    tools:context="test.org.myapp.NextActivity">
    
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello"
        android:textStyle="bold"
        android:textSize="20dp"/>
    
    </LinearLayout>
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Abhishek kumar    7 年前

    首先检查您是否通过放置日志来获取SharedPreference值,如果是,请将代码更改为我的以下代码。

    Log.e("LanguageType", prefs.getString("myLanguage", "")); 
    

    将您的代码更改为我的以下代码,确保它对您有效

    public void loadLanguage() {
    
        SharedPreferences prefs = getSharedPreferences("MY_LANGUAGE", MODE_PRIVATE);
      //  String myLang = prefs.getString("myLanguage", "");
    
        if (prefs.getString("myLanguage", "").equals("fr")) {
            setLanguage("fr");
        } else if (prefs.getString("myLanguage", "").equals("hi")) {
            setLanguage("hi");
        } else if (prefs.getString("myLanguage", "").equals("en")) {
            setLanguage("en");
        }
    
    }