Commit 56cd1959 by Paktalin

Button clear works as it should

parent b68615ab
package com.paktalin.vocabularynotebook;
public class Random {
}
...@@ -13,8 +13,11 @@ import kotlinx.android.synthetic.main.fragment_new_word.* ...@@ -13,8 +13,11 @@ import kotlinx.android.synthetic.main.fragment_new_word.*
class NewWordFragment : Fragment() { class NewWordFragment : Fragment() {
private var etWordEmpty = true private var wordEmpty: Boolean = true
private var etTranslationEmpty = true set(value) { field = value; updateButtons() }
private var translationEmpty: Boolean = true
set(value) { field = value; updateButtons() }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_new_word, container, false) return inflater.inflate(R.layout.fragment_new_word, container, false)
...@@ -22,39 +25,53 @@ class NewWordFragment : Fragment() { ...@@ -22,39 +25,53 @@ class NewWordFragment : Fragment() {
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState) super.onActivityCreated(savedInstanceState)
etWord.addTextChangedListener(object : TextWatcher { etWord.addTextChangedListener(textWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { } wordEmpty = etWord.text.isEmpty() })
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun afterTextChanged(editable: Editable) { etTranslation.addTextChangedListener(textWatcher {
if (!etWord.text.isEmpty()) { translationEmpty = etTranslation.text.isEmpty() })
showCancelButton()
etWordEmpty = false btnClear.setOnClickListener {
} else etWordEmpty = true etWord.text.clear()
if (!etWordEmpty && !etTranslationEmpty) etTranslation.text.clear()
showAddWordButton()
} }
}) }
etTranslation.addTextChangedListener(object : TextWatcher {
private fun textWatcher(setEmpty: () -> Unit): TextWatcher {
return object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { } override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { } override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) { }
override fun afterTextChanged(editable: Editable) { override fun afterTextChanged(editable: Editable) { setEmpty() }
if (!etTranslation.text.isEmpty()) { }
showCancelButton()
etTranslationEmpty = false
} else etTranslationEmpty = true
if (!etWordEmpty && !etTranslationEmpty)
showAddWordButton()
} }
})
private fun updateButtons() {
if (!wordEmpty || !translationEmpty)
showClearButton()
if (!wordEmpty && !translationEmpty)
showAddWordButton()
if (wordEmpty || translationEmpty)
hideAddWordButton()
if (wordEmpty && translationEmpty)
hideClearButton()
} }
private fun showAddWordButton() { private fun showAddWordButton() {
Log.d(TAG, "showAddWordButton")
//todo show add word button //todo show add word button
} }
private fun showCancelButton() { private fun hideAddWordButton() {
Log.d(TAG, "empty word is focused") Log.d(TAG, "hideAddWordButton")
btnClear.setImageResource(R.drawable.ic_cancel_icon) }
private fun hideClearButton() {
Log.d(TAG, "hideClearButton")
btnClear.visibility = View.GONE
}
private fun showClearButton() {
Log.d(TAG, "showClearButton")
btnClear.visibility = View.VISIBLE btnClear.visibility = View.VISIBLE
//todo add button click listener //todo add button click listener
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment