Upgrade to Pro — share decks privately, control downloads, hide ads and more …

社内Android共有定例会資料

RyoWATANABE
July 09, 2018
45

 社内Android共有定例会資料

RyoWATANABE

July 09, 2018
Tweet

Transcript

  1. private fun attemptLogin() { // Reset errors. email.error = null

    password.error = null // Store values at the time of the login attempt. val emailStr = email.text.toString() val passwordStr = password.text.toString() var cancel = false if (!isPasswordValid(passwordStr)) { password.error = "Valid password required." cancel = true } if (!isEmailValid(emailStr)) { email.error = "Valid email address required." cancel = true } if (cancel) { Toast.makeText(this, "ERROR", Toast.LENGTH_SHORT).show() } else { Toast.makeText(this, "LOGIN TASK", Toast.LENGTH_SHORT).show() } }
  2. private fun attemptLogin() { var cancel = false email.validate({ s

    -> s.isValidEmail() }, "Valid email address required.") { cancel = true } password.validate({ s -> s.isValidPassword() }, "Valid password required.") { cancel = true } if (cancel) { Toast.makeText(this, "ERROR", Toast.LENGTH_SHORT).show() return } Toast.makeText(this, "LOGIN TASK", Toast.LENGTH_SHORT).show()
  3. private fun attemptLogin() { var cancel = false email.validate({ s

    -> s.isValidEmail() }, "Valid email address required.") { cancel = true } password.validate({ s -> s.isValidPassword() }, "Valid password required.") { cancel = true } if (cancel) { Toast.makeText(this, "ERROR", Toast.LENGTH_SHORT).show() return } Toast.makeText(this, "LOGIN TASK", Toast.LENGTH_SHORT).show()
  4. fun EditText.afterTextChanged(afterTextChanged: (String) -> Unit) { this.addTextChangedListener(object : TextWatcher {

    override fun afterTextChanged(s: Editable?) { afterTextChanged.invoke(s.toString()) } override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { } override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { } }) } fun EditText.validate(validator: (String) -> Boolean, message: String, errorCallback: () -> Unit) { this.afterTextChanged { this.error = if (validator(it)) null else message } this.error = if (validator(this.text.toString())) null else message if (this.error.isNotEmpty()) { errorCallback.invoke() } }
  5. fun EditText.afterTextChanged(afterTextChanged: (String) -> Unit) { this.addTextChangedListener(object : TextWatcher {

    override fun afterTextChanged(s: Editable?) { afterTextChanged.invoke(s.toString()) } override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { } override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { } }) } fun EditText.validate(validator: (String) -> Boolean, message: String, errorCallback: () -> Unit) { this.afterTextChanged { this.error = if (validator(it)) null else message } this.error = if (validator(this.text.toString())) null else message if (this.error.isNotEmpty()) { errorCallback.invoke() } }
  6. fun EditText.afterTextChanged(afterTextChanged: (String) -> Unit) { this.addTextChangedListener(object : TextWatcher {

    override fun afterTextChanged(s: Editable?) { afterTextChanged.invoke(s.toString()) } override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { } override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { } }) } fun EditText.validate(validator: (String) -> Boolean, message: String, errorCallback: () -> Unit) { this.afterTextChanged { this.error = if (validator(it)) null else message } this.error = if (validator(this.text.toString())) null else message if (this.error.isNotEmpty()) { errorCallback.invoke() } }
  7. Few extensions and utility functions for Kotlin and Android •

    https://github.com/jitinsharma/Kotlin.someExtensions