Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MainActivity : AppCompatActivity() {
- private var isButtonClickProcessing = false
- @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)
- nicknameInputField.addTextChangedListener(object : TextWatcher {
- override fun afterTextChanged(p0: Editable?) {
- }
- override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
- }
- override fun onTextChanged(text: CharSequence?, p1: Int, p2: Int, p3: Int) {
- text?.let { // 입력 내용에 따라 버튼색 변하게 하기
- if (text.length >= 4 && passwordInputField.length() >= 4) {
- okButton.setBackgroundColor(Color.BLUE)
- } else {
- okButton.setBackgroundColor(Color.GRAY)
- }
- }
- }
- })
- passwordInputField.addTextChangedListener(object : TextWatcher {
- override fun afterTextChanged(p0: Editable?) {
- }
- override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
- }
- override fun onTextChanged(text: CharSequence?, p1: Int, p2: Int, p3: Int) {
- text?.let { // 입력 내용에 따라 버튼색 변하게 하기
- if (text.length >= 4 && nicknameInputField.length() >= 4) {
- okButton.setBackgroundColor(Color.BLUE)
- } else {
- okButton.setBackgroundColor(Color.GRAY)
- }
- }
- }
- })
- okButton.setOnClickListener {
- // 버튼 클릭에 쿨타임 걸기
- if(isButtonClickProcessing) return@setOnClickListener
- isButtonClickProcessing = true
- Observable.timer(1, TimeUnit.SECONDS).subscribe { isButtonClickProcessing = false }
- 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()
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement