Skip to content

Instantly share code, notes, and snippets.

@bsscco
Created July 9, 2019 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bsscco/4954a5f497624b458a12e1eefaad6dc6 to your computer and use it in GitHub Desktop.
Save bsscco/4954a5f497624b458a12e1eefaad6dc6 to your computer and use it in GitHub Desktop.
RxBinding 사용 후
class MainActivity : AppCompatActivity() {
private val compositeDisposable = CompositeDisposable()
@SuppressLint("CheckResult")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val nicknameInputField = findViewById<EditText>(R.id.nicknameInputField)
val passwordInputField = findViewById<EditText>(R.id.passwordInputField)
val okButton = findViewById<Button>(R.id.okButton)
compositeDisposable.add(Observable.merge(nicknameInputField.textChanges(), passwordInputField.textChanges())
.subscribe { text -> // 입력 내용에 따라 버튼색 변하게 하기
if (nicknameInputField.length() >= 4 && passwordInputField.length() >= 4) {
okButton.setBackgroundColor(Color.BLUE)
} else {
okButton.setBackgroundColor(Color.GRAY)
}
})
compositeDisposable.add(okButton.clicks()
.throttleFirst(1, TimeUnit.SECONDS) // 버튼 클릭에 쿨타임 걸기
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
when {
nicknameInputField.length() < 4 -> Toast.makeText(this, "닉네임 4글자 이상!", Toast.LENGTH_SHORT).show()
passwordInputField.length() < 4 -> Toast.makeText(this, "패스워드 4글자 이상!", Toast.LENGTH_SHORT).show()
else -> {
nicknameInputField.setText("")
passwordInputField.setText("")
Toast.makeText(this, "OK!", Toast.LENGTH_SHORT).show()
}
}
}
)
}
override fun onDestroy() {
compositeDisposable.clear()
super.onDestroy()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment