2011-03-17 32 views
1

我已经本地化几乎所有的应用程序,只剩下几个异常值。下面是一个例子。我怎样才能做到这一点,而不创建2个独立的@string变量?重构 - Android字符串提取问题

builder.setTitle("Version " + getAppVerName() + " Changes"); 

凡getAppVerName()=

public String getAppVerName() { 
    String text; 
    try { 
     text = getPackageManager().getPackageInfo(getPackageName(), 0).versionName; 
    } catch (NameNotFoundException e) { 
     text = "Version Not Found"; 
    } 
    return text; 
} 

回答

3

可以在strings.xml中格式字符串。

<string name="version">Version %1$s Changes</string> 

builder.setTitle(getString(R.string.version, getAppVerName())); 
+0

我喜欢你的比我的替换更好:)你能给我在%1 $ s上30秒的底漆吗? %1是getAppVerName()的返回值。 $ s是否意味着它是一个字符串? – 2011-03-17 13:45:15

+0

你能详细说明使用%1 $ s吗? – 2011-03-17 16:00:50

+1

s表示其字符串d表示其数字。 1表示它的第一个参数。第二个是%2 $ s。 – 2011-03-17 17:07:51

2

你可以立即返回,但我认为你的代码是很好的方式。

try { 
     return getPackageManager().getPackageInfo(getPackageName(), 0).versionName; 
    } catch (NameNotFoundException e) { 
     return "Version Not Found"; 
    }