Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. class MainActivity : AppCompatActivity() {
  2.  
  3. private val compositeDisposable = CompositeDisposable()
  4.  
  5. @SuppressLint("CheckResult")
  6. override fun onCreate(savedInstanceState: Bundle?) {
  7. super.onCreate(savedInstanceState)
  8. setContentView(R.layout.activity_main)
  9.  
  10. val nicknameInputField = findViewById<EditText>(R.id.nicknameInputField)
  11. val passwordInputField = findViewById<EditText>(R.id.passwordInputField)
  12. val okButton = findViewById<Button>(R.id.okButton)
  13.  
  14. compositeDisposable.add(Observable.merge(nicknameInputField.textChanges(), passwordInputField.textChanges())
  15. .subscribe { text -> // 입력 내용에 따라 버튼색 변하게 하기
  16. if (nicknameInputField.length() >= 4 && passwordInputField.length() >= 4) {
  17. okButton.setBackgroundColor(Color.BLUE)
  18. } else {
  19. okButton.setBackgroundColor(Color.GRAY)
  20. }
  21. })
  22.  
  23. compositeDisposable.add(okButton.clicks()
  24. .throttleFirst(1, TimeUnit.SECONDS) // 버튼 클릭에 쿨타임 걸기
  25. .observeOn(AndroidSchedulers.mainThread())
  26. .subscribe {
  27. when {
  28. nicknameInputField.length() < 4 -> Toast.makeText(this, "닉네임 4글자 이상!", Toast.LENGTH_SHORT).show()
  29. passwordInputField.length() < 4 -> Toast.makeText(this, "패스워드 4글자 이상!", Toast.LENGTH_SHORT).show()
  30. else -> {
  31. nicknameInputField.setText("")
  32. passwordInputField.setText("")
  33. Toast.makeText(this, "OK!", Toast.LENGTH_SHORT).show()
  34. }
  35. }
  36. }
  37. )
  38. }
  39.  
  40. override fun onDestroy() {
  41. compositeDisposable.clear()
  42. super.onDestroy()
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement