Java Android:在mainActivity中单击更改区域设置

huangapple go评论118阅读模式
英文:

Java Android: Change locale onClick in mainActivity

问题

我想让我的应用支持两种语言,英文和意大利文,所以我在MainActivity中创建了一个切换来改变区域设置。想法是更改区域设置并重新启动活动以传递新的语言。我使用API 28来支持更多设备。

我尝试过这个方法,但它不起作用。有时会更改区域设置,但只能一次。

英文:

I want my app support 2 language, en and it, so I create a Switch to change the locale in the mainActivity.
The idea is to change Locale and restart the activity passing the new language.
I use API 28 to support more devices.

I tried this but it doesn't work.
Sometimes change the locale but only once.

  1. @Override
  2. protected void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. activityContext = this.getApplicationContext();
  5. setContentView(R.layout.activity_main);
  6. language = findViewById(R.id.language);
  7. String lan = getIntent().getStringExtra("lan");
  8. if(lan != null) {
  9. if(lan.equals("ita")) {
  10. language.setChecked(true);
  11. lang = "ita";
  12. }else lang = "eng";
  13. }else if(Locale.getDefault().toString().startsWith("it")){
  14. language.setChecked(true);
  15. lang = "ita";
  16. }
  17. buttonPlay.setOnClickListener(view -> enterRoom());
  18. language.setOnCheckedChangeListener((compoundButton, b) -> {
  19. lang = b? "ita" : "eng";
  20. setLocale(lang);
  21. });
  22. }
  23. private void setLocale(String lang){
  24. Locale locale = new Locale(lang.substring(0, 2));
  25. Locale.setDefault(locale);
  26. Resources resources = getResources();
  27. Configuration configuration = resources.getConfiguration();
  28. configuration.setLocale(locale);
  29. getApplicationContext().createConfigurationContext(configuration);
  30. Intent i = getIntent();
  31. i.putExtra("lan",lang);
  32. finish();
  33. startActivity(i);
  34. }

答案1

得分: 0

I've translated the code for you, excluding the code parts:

  1. import android.content.Intent;
  2. import android.content.res.Configuration;
  3. import android.content.res.Resources;
  4. import android.os.Bundle;
  5. import android.widget.Button;
  6. import android.widget.Switch;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. import java.util.Locale;
  9. public class MainActivity extends AppCompatActivity {
  10. private Switch languageSwitch;
  11. private String lang;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. // Get the language preference from the intent or default to the device locale
  16. String lan = getIntent().getStringExtra("lan");
  17. if (lan == null) {
  18. lan = Locale.getDefault().getLanguage();
  19. }
  20. // Set the activity language based on the preference
  21. lang = lan.equals("it") ? "it" : "en";
  22. setLocale(lang);
  23. // Set the activity layout
  24. setContentView(R.layout.activity_main);
  25. // Find views and set up click listeners
  26. languageSwitch = findViewById(R.id.language_switch);
  27. languageSwitch.setChecked(lang.equals("it"));
  28. languageSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
  29. lang = isChecked ? "it" : "en";
  30. setLocale(lang);
  31. });
  32. Button playButton = findViewById(R.id.play_button);
  33. playButton.setOnClickListener(v -> enterRoom());
  34. }
  35. private void setLocale(String lang) {
  36. // Update the app's language configuration
  37. Locale locale = new Locale(lang);
  38. Locale.setDefault(locale);
  39. Resources resources = getResources();
  40. Configuration configuration = resources.getConfiguration();
  41. configuration.setLocale(locale);
  42. getBaseContext().getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
  43. getApplicationContext().getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
  44. // Restart the activity with the new configuration
  45. Intent intent = new Intent(this, MainActivity.class);
  46. intent.putExtra("lan", lang);
  47. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  48. startActivity(intent);
  49. }
  50. private void enterRoom() {
  51. // Start the room activity with the current language preference
  52. Intent intent = new Intent(this, RoomActivity.class);
  53. intent.putExtra("lan", lang);
  54. startActivity(intent);
  55. }
  56. }

Please note that this is a direct translation of the provided code into Chinese, and I haven't made any changes to the code itself.

英文:
  1. import android.content.Intent;
  2. import android.content.res.Configuration;
  3. import android.content.res.Resources;
  4. import android.os.Bundle;
  5. import android.widget.Button;
  6. import android.widget.Switch;
  7. import androidx.appcompat.app.AppCompatActivity;
  8. import java.util.Locale;
  9. public class MainActivity extends AppCompatActivity {
  10. private Switch languageSwitch;
  11. private String lang;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. // Get the language preference from the intent or default to the device locale
  16. String lan = getIntent().getStringExtra("lan");
  17. if (lan == null) {
  18. lan = Locale.getDefault().getLanguage();
  19. }
  20. // Set the activity language based on the preference
  21. lang = lan.equals("it") ? "it" : "en";
  22. setLocale(lang);
  23. // Set the activity layout
  24. setContentView(R.layout.activity_main);
  25. // Find views and set up click listeners
  26. languageSwitch = findViewById(R.id.language_switch);
  27. languageSwitch.setChecked(lang.equals("it"));
  28. languageSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
  29. lang = isChecked ? "it" : "en";
  30. setLocale(lang);
  31. });
  32. Button playButton = findViewById(R.id.play_button);
  33. playButton.setOnClickListener(v -> enterRoom());
  34. }
  35. private void setLocale(String lang) {
  36. // Update the app's language configuration
  37. Locale locale = new Locale(lang);
  38. Locale.setDefault(locale);
  39. Resources resources = getResources();
  40. Configuration configuration = resources.getConfiguration();
  41. configuration.setLocale(locale);
  42. getBaseContext().getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
  43. getApplicationContext().getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
  44. // Restart the activity with the new configuration
  45. Intent intent = new Intent(this, MainActivity.class);
  46. intent.putExtra("lan", lang);
  47. intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
  48. startActivity(intent);
  49. }
  50. private void enterRoom() {
  51. // Start the room activity with the current language preference
  52. Intent intent = new Intent(this, RoomActivity.class);
  53. intent.putExtra("lan", lang);
  54. startActivity(intent);
  55. }
  56. }

Try this sample code separatly and change it with your code.

答案2

得分: 0

经过数小时的尝试,我最终使用updateConfiguration解决了这个问题,尽管它已被弃用。

  1. private void setLocale(String lang) {
  2. Locale.setDefault(new Locale(lang.substring(0, 2)));
  3. Configuration configuration = new Configuration();
  4. configuration.setLocale(locale);
  5. getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
  6. Intent i = getIntent();
  7. i.putExtra("lan", locale);
  8. finish();
  9. startActivity(i);
  10. }
英文:

After hours of trying, I finally fixed this using updateConfiguration even though it's deprecated

  1. private void setLocale(String lang){
  2. Locale.setDefault(new Locale(lang.substring(0, 2)));
  3. Configuration configuration = new Configuration();
  4. configuration.setLocale(locale);
  5. getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
  6. Intent i = getIntent();
  7. i.putExtra("lan",locale);
  8. finish();
  9. startActivity(i);
  10. }

huangapple
  • 本文由 发表于 2023年2月18日 19:54:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75493159.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定