只是想知道是否有任何有关使用TextWatchers,OnClickListeners等方面的最佳做法,关于如何将它们放在代码中。TextWatchers,OnClickListeners等
你做到以下几点:
textBox.addTextChangedListener(new TextWatcher()
{
public void afterTextChanged(Editable s)
{
// some code here
}
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// and/or here
}
public void onTextChanged(CharSequence s, int start, int before, int count)
{
// and/or even here
}
});
或者这将是最好有TextWatcher等作为变量,并调用它像:
textBox.addTextChangedListener(myTextWatcher);
我理解的第二会好得多对于可重用性,但很好奇,如果在这个例子中只有一个实例,TextWatcher。
感谢您提供此信息。详细和易于遵循。当我得到一些时间的时候,我会看看ButterKnife。再次感谢! – 2014-10-10 13:22:17
没问题。我认为在你的代码中将接口变成像'myTextWatcher'这样的变量会使代码更难读。如果你多次使用同一个观察器,这可能是有用的。在这种情况下,实现与主类的接口并使用'this'作为变量也是有意义的。 – tasomaniac 2014-10-10 13:25:20