首页 > 编程知识 正文

kotlin现在必用吗(kotlin语言教程)

时间:2023-05-04 11:14:22 阅读:77209 作者:2723

today there’S1 pullrequestthatattractmyattention.thiscodeispartofasimplecalculatoractivity.itlistentoafieldtocalculatethethetity

今天有一个请求引起了我的注意。 这个代码是简单计算机活动的一部分。 侦听字段以计算总公称值,并将其显示在TextView中。

sera tus _ ribu.addtextchangedlistener (object : text watcher ) overridefunaftertextchanged (s 3360可编辑) } overridefunbeforetextchanged (s : char sequence,start: Int,count: Int,after 3360 int (overrridefunontextchanged ) ) start: Int,before: Int, count : int (valsumpaidinstallment=sumpaidinstallment ) valtxtnominal=' $ { display helper.format currency } sumpaincy idinstallment ) }' angsuran.text=txtNominal } } ) repeatthiscodefor 11 fieldandyougotalargecodewithlotofemptyspaceandemptyiityior there’snothingyoucando.youjustacceptit.butwithkotlin,codelikethisbecomemoreandmoreunacceptable。

对11个字段重复此代码可以获得较大的代码。 此代码包含许多可用空间和空实现。 在Java中,什么也做不了。 你只接受那个。 但是使用kotlin,这样的代码越来越难接受。

tofixitwecanleveragelambdainkotlin.soicreatedadynamictextwatcherclassthatextendtextwatcherandimplementedallthemethod.this chis athateachcorrelatewithatextwatcherfunction.thiswayyoucanjustpassyourfunctionwithouthavingtoimplemention

要解决这个问题,请使用Kotlin的lambda。 因此,我们创建了一个扩展了TextWatcher并实现了所有方法的DynamicTextWatcher类。 这样的构造函数接收三个lambda,每个lambda都与一个文本观察器函数相关联。 这允许传递函数而不实现其他空函数。

classdynamictextwatcher (私有平衡交换: ) editable? - Unit,隐私保护: (char sequence?Int,Int,Int )- Unit,privatevalonchanged : (char sequence?Int、

Int, Int) -> Unit) : TextWatcher { override fun afterTextChanged(s: Editable?) { afterChanged(s) } override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { beforeChanged(s, start, count, after) } override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { onChanged(s, start, before, count) }}

But this implementation still requires you to pass empty lambda for the function that you didn’t use. We can again leverage kotlin feature by making the parameter as optional parameter. The only thing we need to do is giving the parameters default value.

但是此实现仍然需要您为未使用的函数传递空的lambda。 通过将参数设为可选参数,我们可以再次利用kotlin功能。 我们唯一需要做的就是给参数提供默认值。

class DynamicTextWatcher( private val afterChanged: ((Editable?) -> Unit) = {}, private val beforeChanged: ((CharSequence?, Int, Int, Int) -> Unit) = { _, _, _, _ -> }, private val onChanged: ((CharSequence?, Int, Int, Int) -> Unit) = { _, _, _, _ -> }) : TextWatcher { override fun afterTextChanged(s: Editable?) { afterChanged(s) } override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { beforeChanged(s, start, count, after) } override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { onChanged(s, start, before, count) }}

Thus now the code can be called like this

因此,现在可以像这样调用代码

seratus_ribu.addTextChangedListener(DynamicTextWatcher( onChanged = { _, _, _, _ -> val sumPaidInstallment = sumPaidInstallment() val txtNominal = "${DisplayHelper.formatCurrency(sumPaidInstallment)} / ${DisplayHelper.formatCurrency( totalPaidInstallment )}" angsuran.text = txtNominal } ))

A whole lot of line has gone. Code that doesn’t matter, boiler plate. Now we an focus directly to the code that matters.

很多行已经走了。 没关系的代码,样板。 现在,我们直接将重点放在重要的代码上。

Turn out the original coder has done a good job of centralizing the calculation. So we can extract the onChanged into a single val resulting the final code to become like this

原来的编码器在集中计算方面做得很好。 因此,我们可以将onChanged提取到单个val中,从而使最终代码变得像这样

.... val nominalWatcher = DynamicTextWatcher( onChanged = { _, _, _, _ -> updateNominal(totalPaidInstallment) }) seratus_ribu.addTextChangedListener(nominalWatcher) lima_puluh_ribu.addTextChangedListener(nominalWatcher) dua_puluh_ribu.addTextChangedListener(nominalWatcher) sepuluh_ribu.addTextChangedListener(nominalWatcher) lima_ribu.addTextChangedListener(nominalWatcher) dua_ribu.addTextChangedListener(nominalWatcher) seribu.addTextChangedListener(nominalWatcher) seribu_logam.addTextChangedListener(nominalWatcher) lima_ratus.addTextChangedListener(nominalWatcher) dua_ratus.addTextChangedListener(nominalWatcher) seratus.addTextChangedListener(nominalWatcher) } private fun updateNominal(totalPaidInstallment: Int) { val sumPaidInstallment = sumPaidInstallment() val txtNominal = "${DisplayHelper.formatCurrency(sumPaidInstallment)} / ${DisplayHelper.formatCurrency( totalPaidInstallment )}" angsuran.text = txtNominal }

138 line of code reduced to 24 line in minutes.

138行代码在几分钟内减少到24行。

They say programming language is only a tool. But choosing the right tool can save you a lot of work.

他们说编程语言只是一种工具。 但是选择正确的工具可以节省大量工作。

Turn out this already exist as Android-ktx implementation. The implementation is quite similar. So it’s interesting to see the difference.

原来这已经作为Android-ktx实现存在。 实现非常相似。 因此,看到差异很有趣。

See the ktx implementation here. To use the ktx implementation the code needs to be modified to be like this:

在此处查看ktx实现。 要使用ktx实现,需要修改代码,如下所示:

val nominalWatcher = { _:CharSequence?, _: Int, _: Int, _: Int -> updateNominal(totalPaidInstallment) } seratus_ribu.addTextChangedListener(onTextChanged = nominalWatcher) seratus_ribu.addTextChangedListener(onTextChanged = nominalWatcher) lima_puluh_ribu.addTextChangedListener(onTextChanged = nominalWatcher) dua_puluh_ribu.addTextChangedListener(onTextChanged = nominalWatcher) sepuluh_ribu.addTextChangedListener(onTextChanged = nominalWatcher) lima_ribu.addTextChangedListener(onTextChanged = nominalWatcher) dua_ribu.addTextChangedListener(onTextChanged = nominalWatcher) seribu.addTextChangedListener(onTextChanged = nominalWatcher) seribu_logam.addTextChangedListener(onTextChanged = nominalWatcher) lima_ratus.addTextChangedListener(onTextChanged = nominalWatcher) dua_ratus.addTextChangedListener(onTextChanged = nominalWatcher) seratus.addTextChangedListener(onTextChanged = nominalWatcher)

翻译自: https://medium.com/@abangkis/creating-dynamic-android-textwatcher-using-kotlin-fb5a4bc24996

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。