android 按“返回”按钮时语言不改变

tkclm6bt  于 2023-06-20  发布在  Android
关注(0)|答案(1)|浏览(120)

我试图实现一个多语言的应用程序一切正常工作我有2个活动第一个是welocome活动第二个是选择语言activity但当我改变语言并按下goBack按钮欢迎活动的langauge不改变这里是我的代码:欢迎活动

public class GetStartedActivity extends AppCompat {
private Button getStartedBtn;
private Bundle b;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_get_started);
    init();

    getStartedBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(GetStartedActivity.this,ChooseLanguageActivity.class));
            overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
        }
    });
}


private void init()
{
    getStartedBtn = findViewById(R.id.get_started_btn);
}

}
选择语言活动:

public class ChooseLanguageActivity extends AppCompat {
    private RadioButton radioButton1;
    private RadioButton radioButton2;
    private RadioButton radioButton3;
    private RadioGroup radioGroup;
    private Button nextBtn;
    private String selectedLang;
    private SharedPreferences sharedPreferences;
    private LanguageManager languageManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_language);
        init();
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.enRBtn) {
                    radioButton1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uk, 0, R.drawable.checkmark, 0);
                    radioButton2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.fr, 0, 0, 0);
                    radioButton3.setCompoundDrawablesWithIntrinsicBounds(R.drawable.dz, 0, 0, 0);
                    selectedLang="en";
                    languageManager.setLang(selectedLang);
                } else if (checkedId == R.id.frRBtn) {
                    radioButton1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uk, 0, 0, 0);
                    radioButton2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.fr, 0, R.drawable.checkmark, 0);
                    radioButton3.setCompoundDrawablesWithIntrinsicBounds(R.drawable.dz, 0, 0, 0);
                    selectedLang="fr";
                    languageManager.setLang(selectedLang);
                } else if (checkedId == R.id.arRBtn) {
                    radioButton1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uk, 0, 0, 0);
                    radioButton2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.fr, 0, 0, 0);
                    radioButton3.setCompoundDrawablesWithIntrinsicBounds(R.drawable.dz, 0, R.drawable.checkmark, 0);
                    selectedLang="ar";
                    languageManager.setLang(selectedLang);
                }
            }
        });
        nextBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                languageManager.setLang(selectedLang);
                startActivity(new Intent(ChooseLanguageActivity.this,SignInActivity.class));

            }
        });
    }
    

    private void init()
    {
        radioGroup = findViewById(R.id.languageRadioGroup);
        radioButton1 = findViewById(R.id.enRBtn);
        radioButton2 = findViewById(R.id.frRBtn);
        radioButton3 = findViewById(R.id.arRBtn);
        nextBtn = findViewById(R.id.lNextBtn);
        languageManager = new LanguageManager(this);
        selectedLang=languageManager.getLang();
        if(selectedLang.equals("en"))
        {
            radioButton1.setChecked(true);
            radioButton1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uk, 0, R.drawable.checkmark, 0);
            radioButton2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.fr, 0, 0, 0);
            radioButton3.setCompoundDrawablesWithIntrinsicBounds(R.drawable.dz, 0, 0, 0);
        } else if (selectedLang.equals("fr"))
        {
            radioButton2.setChecked(true);
            radioButton1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uk, 0, 0, 0);
            radioButton2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.fr, 0, R.drawable.checkmark, 0);
            radioButton3.setCompoundDrawablesWithIntrinsicBounds(R.drawable.dz, 0, 0, 0);
        }else
        {
            radioButton3.setChecked(true);
            radioButton1.setCompoundDrawablesWithIntrinsicBounds(R.drawable.uk, 0, 0, 0);
            radioButton2.setCompoundDrawablesWithIntrinsicBounds(R.drawable.fr, 0, 0, 0);
            radioButton3.setCompoundDrawablesWithIntrinsicBounds(R.drawable.dz, 0, R.drawable.checkmark, 0);
        }
    }
}

语言管理器类:

public class LanguageManager {
    private Context ctx;
    private SharedPreferences sharedPreferences;
    public LanguageManager(Context ctx) {
        this.ctx = ctx;
        sharedPreferences=ctx.getSharedPreferences("LANG",Context.MODE_PRIVATE);
    }
    public void updateLang(String code)
    {
        Locale locale = new Locale(code);
        Locale.setDefault(locale);
        Resources resources = ctx.getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.locale=locale;
        resources.updateConfiguration(configuration,resources.getDisplayMetrics());
        setLang(code);
    }

    public String getLang()
    {
        return sharedPreferences.getString("lang","en");
    }
    public void setLang(String code)
    {
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString("lang",code);
        editor.commit();
    }
}

AppCompat类:

public class AppCompat extends AppCompatActivity
{
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LanguageManager languageManager = new LanguageManager(this);
        languageManager.updateLang(languageManager.getLang());

    }
}
xxslljrj

xxslljrj1#

尝试AppCompat的以下代码,它只覆盖onResume()

public class AppCompat extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LanguageManager languageManager = new LanguageManager(this);
    languageManager.updateLang(languageManager.getLang());

}

@Override
protected void onResume() {
    super.onResume();
    LanguageManager languageManager = new LanguageManager(this);
    languageManager.updateLang(languageManager.getLang());
}
}

相关问题