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

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

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.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityContext = this.getApplicationContext();
        setContentView(R.layout.activity_main);

        language = findViewById(R.id.language);


        String lan = getIntent().getStringExtra("lan");
        if(lan != null) {
            if(lan.equals("ita")) {
                language.setChecked(true);
                lang = "ita";
            }else lang = "eng";
        }else if(Locale.getDefault().toString().startsWith("it")){
            language.setChecked(true);
            lang = "ita";
        }

        buttonPlay.setOnClickListener(view -> enterRoom());
        language.setOnCheckedChangeListener((compoundButton, b) -> {
            lang = b? "ita" : "eng";
            setLocale(lang);
        });
    }

    private void setLocale(String lang){
        Locale locale = new Locale(lang.substring(0, 2));
        Locale.setDefault(locale);
        Resources resources = getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(locale);
        getApplicationContext().createConfigurationContext(configuration);
        Intent i = getIntent();
        i.putExtra("lan",lang);
        finish();
        startActivity(i);
    }

答案1

得分: 0

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

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Switch;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    private Switch languageSwitch;
    private String lang;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the language preference from the intent or default to the device locale
        String lan = getIntent().getStringExtra("lan");
        if (lan == null) {
            lan = Locale.getDefault().getLanguage();
        }

        // Set the activity language based on the preference
        lang = lan.equals("it") ? "it" : "en";
        setLocale(lang);

        // Set the activity layout
        setContentView(R.layout.activity_main);

        // Find views and set up click listeners
        languageSwitch = findViewById(R.id.language_switch);
        languageSwitch.setChecked(lang.equals("it"));
        languageSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
            lang = isChecked ? "it" : "en";
            setLocale(lang);
        });

        Button playButton = findViewById(R.id.play_button);
        playButton.setOnClickListener(v -> enterRoom());
    }

    private void setLocale(String lang) {
        // Update the app's language configuration
        Locale locale = new Locale(lang);
        Locale.setDefault(locale);
        Resources resources = getResources();
        Configuration configuration = resources.getConfiguration();
        configuration.setLocale(locale);
        getBaseContext().getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
        getApplicationContext().getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());

        // Restart the activity with the new configuration
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("lan", lang);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    private void enterRoom() {
        // Start the room activity with the current language preference
        Intent intent = new Intent(this, RoomActivity.class);
        intent.putExtra("lan", lang);
        startActivity(intent);
    }
}

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.

英文:
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Switch;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private Switch languageSwitch;
private String lang;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the language preference from the intent or default to the device locale
String lan = getIntent().getStringExtra("lan");
if (lan == null) {
lan = Locale.getDefault().getLanguage();
}
// Set the activity language based on the preference
lang = lan.equals("it") ? "it" : "en";
setLocale(lang);
// Set the activity layout
setContentView(R.layout.activity_main);
// Find views and set up click listeners
languageSwitch = findViewById(R.id.language_switch);
languageSwitch.setChecked(lang.equals("it"));
languageSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> {
lang = isChecked ? "it" : "en";
setLocale(lang);
});
Button playButton = findViewById(R.id.play_button);
playButton.setOnClickListener(v -> enterRoom());
}
private void setLocale(String lang) {
// Update the app's language configuration
Locale locale = new Locale(lang);
Locale.setDefault(locale);
Resources resources = getResources();
Configuration configuration = resources.getConfiguration();
configuration.setLocale(locale);
getBaseContext().getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
getApplicationContext().getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
// Restart the activity with the new configuration
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("lan", lang);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
private void enterRoom() {
// Start the room activity with the current language preference
Intent intent = new Intent(this, RoomActivity.class);
intent.putExtra("lan", lang);
startActivity(intent);
}
}

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

答案2

得分: 0

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

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

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

private void setLocale(String lang){
Locale.setDefault(new Locale(lang.substring(0, 2)));
Configuration configuration = new Configuration();
configuration.setLocale(locale);
getResources().updateConfiguration(configuration, getResources().getDisplayMetrics());
Intent i = getIntent();
i.putExtra("lan",locale);
finish();
startActivity(i);
}

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:

确定