如何通过单击事件更改我所有活动的所有文本(按钮文本、文本视图等)?

How to change all the text on all my activities (button text, textviews, etc.) with a click event?

当我单击一个按钮或 select 一个菜单项时,我可以将应用程序中的所有文本更改为其他文本吗?

我实际上想做的是让我的应用程序翻译成几种非 iso 语言("non-iso" 意味着我无法在 wiki 的 ISO-639 列表中找到它们)。

所以我希望用户能够 select 他们查看我的应用程序时使用的语言,无论他们的 phone 语言设置如何。我希望他们能够从应用程序中创建语言 selection。

我在 strings.xml 中拥有所有英文按钮和文本视图文本,我可以为新语言制作一个字符串 -xx.xml。

我将使用列出语言选项的子菜单。因此,文本更改将响应该菜单上的 onclick 事件。 非常欢迎任何指点。

取自:How to change language of app when user selects language?

public void setLocale(String lang) { 
    myLocale = new Locale(lang); 
    Resources res = getResources(); 
    DisplayMetrics dm = res.getDisplayMetrics(); 
    Configuration conf = res.getConfiguration(); 
    conf.locale = myLocale; 
    res.updateConfiguration(conf, dm); 
    Intent refresh = new Intent(this, YOURACTIVITY.class); 
    startActivity(refresh); 
    finish();
} 

您可以将任何有效的语言字符串传递给 setLocale 方法,例如 "de" 或 "it",但您必须重新启动 Activity。

我不知道如果您按下后退按钮,您的应用程序会如何运行。如果它启动了您的 "old" Activity,请在重新启动 Activity 时尝试此操作:

public void setLocale(String lang) { 
    myLocale = new Locale(lang); 
    Resources res = getResources(); 
    DisplayMetrics dm = res.getDisplayMetrics(); 
    Configuration conf = res.getConfiguration(); 
    conf.locale = myLocale; 
    res.updateConfiguration(conf, dm); 
    Intent refresh = new Intent(this, YOURACTIVITY.class);
    refresh.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(refresh); 

}