Created
January 30, 2021 18:21
-
-
Save SurajBahadur/69d495b421e65f7902b2eeeb510719bb to your computer and use it in GitHub Desktop.
TextWatcher For More Than One EditText
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AdsFormActivity : AppCompatActivity() { | |
private lateinit var binding: ActivityAdsForm2Binding | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
binding = DataBindingUtil.setContentView(this, R.layout.activity_ads_form) | |
supportActionBar?.hide() | |
setClickListeners() | |
setTextWatchers() | |
} | |
private fun setTextWatchers() { | |
MultiTextWatcher() | |
.registerEditText(binding.etCompanyAddress) | |
.registerEditText(binding.etCompanyIntro) | |
.registerEditText(binding.etCompanyName) | |
.registerEditText(binding.etCompanyPhone) | |
.setCallback(object : MultiTextWatcher.TextWatcherWithInstance { | |
override fun beforeTextChanged(editText: EditText, s: CharSequence, start: Int, count: Int, after: Int) { | |
} | |
override fun onTextChanged(editText: EditText, s: CharSequence, start: Int, before: Int, count: Int) { | |
when (editText) { | |
binding.etCompanyAddress -> { | |
//do your logic here | |
} | |
binding.etCompanyPhone -> { | |
//do your logic here and so on | |
} | |
} | |
} | |
override fun afterTextChanged(editText: EditText, editable: Editable) { | |
} | |
}) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MultiTextWatcher { | |
private var callback: TextWatcherWithInstance? = null | |
fun setCallback(callback: TextWatcherWithInstance): MultiTextWatcher { | |
this.callback = callback | |
return this | |
} | |
fun registerEditText(editText: EditText): MultiTextWatcher { | |
editText.addTextChangedListener(object : TextWatcher { | |
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) { | |
callback!!.beforeTextChanged(editText, s, start, count, after) | |
} | |
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) { | |
callback!!.onTextChanged(editText, s, start, before, count) | |
} | |
override fun afterTextChanged(editable: Editable) { | |
callback!!.afterTextChanged(editText, editable) | |
} | |
}) | |
return this | |
} | |
interface TextWatcherWithInstance { | |
fun beforeTextChanged(editText: EditText, s: CharSequence, start: Int, count: Int, after: Int) | |
fun onTextChanged(editText: EditText, s: CharSequence, start: Int, before: Int, count: Int) | |
fun afterTextChanged(editText: EditText, editable: Editable) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment