안드로이드/Android View

안드로이드 Edittext Textwatcher 끊길때

코딩하는후운 2022. 10. 27. 10:21
반응형
val tradeConditionObservable = Observable.create<CharSequence>{emitter ->
            etTradeConditionContents.addTextChangedListener(object: TextWatcher {
                var str = ""
                override fun afterTextChanged(s: Editable?) {

                }

                override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                }

                override fun onTextChanged(s: CharSequence?, p1: Int, p2: Int, p3: Int) {
                    s?.let {
                        emitter.onNext(it)
                    }
                }
            })
        }

        tradeConditionObservable
            .debounce(500L, TimeUnit.MILLISECONDS)
            .filter{ !it.isNullOrEmpty()}
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                val length = it.toString().toByteArray(Charset.forName("UTF-8")).size
                tvTradeConditionCount.text = String.format(R.string.orderregist_tradecount_contents.toResString(), length)
                if (length > 300) {
                    etTradeConditionContents.setText(strTradeCondition)
                    etTradeConditionContents.setSelection(strTradeCondition.length)
                    tvTradeConditionCount.text = String.format(R.string.orderregist_tradecount_contents.toResString(), strTradeCondition.toByteArray(Charset.forName("UTF-8")).size)
                }
                else{
                    strTradeCondition = it.toString()
                }
            }
반응형