小编Jul*_*ard的帖子

Android更改并在应用内设置默认语言环境

我正在研究Android应用程序的全球化.我必须提供从应用程序中选择不同区域设置的选项.我在我的活动(HomeActivity)中使用以下代码,其中我提供了更改语言环境的选项

Configuration config = new Configuration();
config.locale = selectedLocale; // set accordingly 
// eg. if Hindi then selectedLocale = new Locale("hi");
Locale.setDefault(selectedLocale); // has no effect
Resources res = getApplicationContext().getResources();
res.updateConfiguration(config, res.getDisplayMetrics());
Run Code Online (Sandbox Code Playgroud)

只要没有像屏幕旋转那样的配置更改,其中locale默认为android系统级别语言环境而不是代码设置的语言环境,这样就可以正常工作.

Locale.setDefault(selectedLocale);
Run Code Online (Sandbox Code Playgroud)

我能想到的一个解决方案是使用SharedPreferences持久保存用户选择的语言环境,并且在每个活动的onCreate()方法中将语言环境设置为持久语言环境,因为onCreate()会在每次配置更改时反复调用.有没有更好的方法来做到这一点,所以我不必在每个活动中都这样做.

基本上我想要的是 - 一旦我在我的HomeActivity中更改/设置为某个区域设置,我希望我的应用程序中的所有活动都使用该区域设置本身而不管任何配置更改....除非并且直到将其更改为其他区域设置应用程序的HomeActivity提供更改区域设置的选项.

java globalization android locale localization

20
推荐指数
1
解决办法
2万
查看次数

python中缀前进管道

我正在尝试实现一个前向管道功能,比如bash |或R最近%>%.我已经看到了这个实现http://dev-tricks.net/pipe-in​​fix-syntax-for-python,但这要求我们事先定义可能与管道一起使用的所有函数.在寻找完全一般的东西时,这是我到目前为止所想到的.

此函数将其第一个参数应用于其第二个参数(函数)

def function_application(a,b):
    return b(a)
Run Code Online (Sandbox Code Playgroud)

例如,如果我们有一个平方函数

def sq(s):
    return s**2
Run Code Online (Sandbox Code Playgroud)

我们可以用这种繁琐的方式调用该函数function_application(5,sq).为了更接近前向管道,我们希望使用function_application中缀表示法.

由此绘制,我们可以定义一个Infix类,以便我们可以将函数包装在特殊字符中,例如|.

class Infix:
    def __init__(self, function):
        self.function = function
    def __ror__(self, other):
        return Infix(lambda x, self=self, other=other: self.function(other, x))
    def __or__(self, other):
        return self.function(other)
Run Code Online (Sandbox Code Playgroud)

现在我们可以定义我们的管道,它只是函数的中缀版本function_application,

p = Infix(function_application)
Run Code Online (Sandbox Code Playgroud)

所以我们可以做这样的事情

5 |p| sq
25
Run Code Online (Sandbox Code Playgroud)

要么

[1,2,3,8] |p| sum |p| sq
196
Run Code Online (Sandbox Code Playgroud)

在那个冗长的解释之后,我的问题是,是否有任何方法可以覆盖有效函数名称的限制.在这里,我已经命名了管道p,但是可以重载非字母数字字符吗?我可以命名一个功能,>所以我的管道是|>|

python infix-notation

4
推荐指数
1
解决办法
1242
查看次数