我正在开始一个使用AppCompat/ActionBarCompatin v7支持库的新项目.我试图找出如何getSupportActionBar在片段中使用from.我承载片段的活动扩展了ActionBarActivity,但我没有看到片段的类似支持类.
从我的片段中
public class CrimeFragment extends Fragment {
//...
getActivity().getSupportActionBar().setSubtitle(R.string.subtitle); // getSupportActionBar is not defined in the v4 version of Fragment
//...
}
Run Code Online (Sandbox Code Playgroud)
使用它的谷歌页面(http://android-developers.blogspot.in/2013/08/actionbarcompat-and-io-2013-app-source.html)表示v4片段应该没有变化.我是否需要将所有getActivity()电话投入ActionBarActivity?这似乎是糟糕的设计.
android android-fragments android-support-library android-actionbar-compat
我正在对Angular和React进行比较,并决定尝试性能测试,看看大型(ish)列表在两个框架中的呈现速度有多快.
当我用我的React原型完成一些基本的货币格式化时,在我的快速笔记本电脑上渲染需要大约2秒钟.使用Angular它几乎不会引人注意 - 只有当我切换到我的手机时它才有明显的延迟.
这是非常令人惊讶的,因为我被告知React应该击败Angular的裤子以获得性能,但在这种情况下似乎相反.
我将我的原型提炼为一个非常简单的应用程序,试图找出问题:https://github.com/pselden/react-render-test
在这个示例中,在更改语言后渲染这个简单的列表需要将近200毫秒,而我几乎没有做任何事情.
我在这里做错了吗?
/** @jsx React.DOM */
'use strict';
var React = require('react'),
Numbers = require('./Numbers');
var numbers = []
for(var i = 0; i < 2000; ++i){
numbers.push(i);
}
var App = React.createClass({
getInitialState: function() {
return { locale: 'en-US' };
},
_onChangeLocale: function(event) {
this.setState({locale: event.target.value});
},
render: function() {
var currentLocale = this.state.locale;
return (
<div>
<select
onChange={this._onChangeLocale}>
<option value="en-US">English</option>
<option value="fr-FR">French</option>
</select>
<Numbers numbers={numbers} locales={[currentLocale]} />
</div>
); …Run Code Online (Sandbox Code Playgroud) 我对autofac很新,所以我可能会完全滥用它.
假设我有一个具有这种结构的类:
public class HelperClass : IHelperClass
{
public HelperClass(string a, string b)
{
this.A = a;
this.B = b;
}
}
Run Code Online (Sandbox Code Playgroud)
我有两个使用该类的类,但构造函数需要不同的默认值.第二个构造函数仅用于测试目的 - 我们总是希望在"真实"应用程序中使用HelperClass:
public class DoesSomething: IDoesSomething
{
public DoesSomething()
: this(new HelperClass("do", "something"));
{
}
internal DoesSomething(IHelperClass helper)
{
this.Helper = helper;
}
}
public class DoesSomethingElse : IDoesSomethingElse
{
public DoesSomethingElse()
: this(new HelperClass("does", "somethingelse"));
{
}
internal DoesSomethingElse(IHelperClass helper)
{
this.Helper = helper;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的AutoFac模块:
public class SomethingModule: Module
{
protected override void Load(ContainerBuilder …Run Code Online (Sandbox Code Playgroud) 我试图想出一些类似于Google日历(甚至是一些gmail消息)的内容,其中自由格式文本将被解析并转换为特定的日期/时间.
一些例子(为简单起见,现在是2013年1月1日凌晨1点):
"I should call Mom tomorrow to wish her a happy birthday" -> "tomorrow" = "2013-01-02"
"The super bowl is on Feb 3rd at 6:30pm" -> "Feb 3rd at 6:30" => "2013-02-03T06:30:00Z"
"Remind me to take out the trash on Friday" => "Friday" => "2013-01-04"
Run Code Online (Sandbox Code Playgroud)
首先,我会问这个问题 - 是否存在任何已经存在的开源库(或其中的一部分).如果没有,你认为我应该采取什么样的方法?
我正在考虑几种不同的可能性:
回到我使用RoboGuice时,我能够将构造函数注入到我的类中,而RoboGuice会选择适当的上下文(注入一个Activity会有Activity上下文,注入Application会有当前的应用程序上下文,注入片段会有片段的活动的上下文等...).
Dagger是否有类似的方法来实现这一目标?
public class Thing {
@Inject
public class Thing(Context context){
// if i'm injected in an Activity, I should be the current activity's context
// if i'm injected in an Fragment, I should be the fragment's activity context
// if i'm injected in a Service, I should be the service's context
// etc...
}
}
Run Code Online (Sandbox Code Playgroud) 我很难找到一种方法,使用Gradle为我的BuildConfig添加多行.看来,当我第二次调用buildConfig时,第一次消失.
我最初是从另一个地方添加这个buildConfig,但是如果我这样做的话,它能够创建一个最小的可重现测试:
buildTypes {
debug {
versionNameSuffix "-DEBUG"
buildConfig "public static final int THING_ONE = 1;"
buildConfig "public static final int THING_TWO = 2;"
}
release {
zipAlign true
buildConfig "public static final int THING_ONE = 3;"
buildConfig "public static final int THING_TWO = 4;"
}
}
Run Code Online (Sandbox Code Playgroud)
然后当我尝试在代码中使用它时:
public class Thing {
public static final int THING = com.example.BuildConfig.THING_ONE + com.example.BuildConfig.THING_TWO;
}
Run Code Online (Sandbox Code Playgroud)
我会收到这个错误:
/Example/src/main/java/com/example/Thing.java:2: cannot find symbol
symbol : variable THING_ONE
location: class com.example.BuildConfig
public static final int THING = …Run Code Online (Sandbox Code Playgroud) 我还是Dagger的新手并试图抓住一些东西.我想将我的模块分成逻辑组,每个组都提供自己的功能,但基本上就像在一个模块中一样.
例如,假设我的主应用程序模块定义如下:
//com.example.android.MyAppModule.java
@Module(
includes = AnalyticsModule.class,
injects = { <snip> }
)
public class MyAppModule {
// various provides
}
Run Code Online (Sandbox Code Playgroud)
我有另一个像这样定义的模块,它设置一个ErrorReporter接口并为它提供具体的实现.
// com.example.android.analytics.AnalyticsModule.java
@Module(
addsTo = MyAppModule.class,
injects = { MyApp.class }
)
public class AnalyticsModule(){
// ErrorReporter is a public interface and ErrorReporterImpl is a package-local final concrete class that implements it
@Provides @Singleton
ErrorReporter providesErrorReporter(ErrorReporterImpl reporter) { return reporter };
}
Run Code Online (Sandbox Code Playgroud)
在我的Application类中,我设置了如下对象图:
// com.example.android.MyApp.java
public class MyApp extends Application {
@Inject ErrorReporter errorReporter;
@Override
public void onCreate() …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写具有以下限制的用户名验证:
以下示例有效:abc123,my_name,12345a
以下示例无效:123456,my_name!,_ 1235
我找到了一些关于使用正面先行词来形容字母约束的东西:(?=.*[a-zA-Z]),看起来第二个约束可能会出现某种负面的前瞻,但我不知道如何把它们混合成一个正则表达式.(注意......我不清楚.*部分在前瞻中的作用......)
它是这样的:/(?=.*[a-zA-Z])(?!.*[^a- zA-Z0-9_])/
编辑:
因为问题要求正则表达式,我接受的答案是:
/^[a-zA-Z0-9_]*[a-zA-Z][a-zA-Z0-9_]*$/
Run Code Online (Sandbox Code Playgroud)
然而,我实际要实现的是Bryan Oakley建议将其分成多个较小的支票.这样,在需求变化的情况下,将来更容易阅读和扩展.谢谢大家!
因为我用ruby-on-rails标记了这个,我将包含我实际使用的代码:
validate :username_format
def username_format
has_one_letter = username =~ /[a-zA-Z]/
all_valid_characters = username =~ /^[a-zA-Z0-9_]+$/
errors.add(:username, "must have at least one letter and contain only letters, digits, or underscores") unless (has_one_letter and all_valid_characters)
end
Run Code Online (Sandbox Code Playgroud) 在Rails 3.1中,可以选择启用HTTP流式处理,以便可以将页面放入块中.在关于此功能的Railscast中,Ryan建议最好启用此功能,以便在仍然呈现页面的其余部分时可以下拉CSS和JavaScript.
我总是遵循指南,在加载所有页面内容之后脚本应位于页面底部,这样可以减少感知的加载时间,但这样做不会利用HTTP流.
您认为现在的最佳做法是什么?
我收到瑞典用户的错误报告,说我们的瑞典货币使用了错误的小数分隔符.
NumberFormat enUS = NumberFormat.getCurrencyInstance(Locale.US);
NumberFormat enGB = NumberFormat.getCurrencyInstance(Locale.UK);
NumberFormat svSE = NumberFormat.getCurrencyInstance(new Locale("sv", "SE"));
double cost = 1020d;
String fmt = "en_US: %s en_GB %s sv_SE %s";
String text = String.format(fmt, enUS.format(cost), enGB.format(cost), svSE.format(cost));
Log.e("Format", text);
> Format? en_US: $1,020.00 en_GB £1,020.00 sv_SE 1 020:00 kr
Run Code Online (Sandbox Code Playgroud)
他们说格式应为"1 020,00 kr".当我检查格式对象时,它看起来在符号表中有","的decimalSeparator,但是":"的"currencySeparator".
有谁知道:实际上是否正确,这是Android/java中的错误还是任何形式的解决方法?