Skip to content

Instantly share code, notes, and snippets.

@MizzleDK
Created February 13, 2019 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save MizzleDK/508bec5156110fcd7ab07c7946bc3e7c to your computer and use it in GitHub Desktop.
Save MizzleDK/508bec5156110fcd7ab07c7946bc3e7c to your computer and use it in GitHub Desktop.
open class AwesomeFragment(var name: String): Fragment(), TextWatcher {
public var textToPrependOnTVAfterTextChanged = "Hello "
init {
// Make sure we don't crash if name is null
name = name ?: ""
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
return View.inflate(activity!!, R.id.awesome_layout, null) // Inflate our awesome layout
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
with (view.findViewById<TextView>(R.layout.textView1)) {
// We want to prepend some awesome text after the initial use of setText()
addTextChangedListener(this@AwesomeFragment)
TV = this
}
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
// Set text
TV.apply { setText("${name}") }
}
override fun onPause() {
super.onResume()
// Clean up our references to the TextView field
TV.removeTextChangedListener(this)
TV = null
}
override fun afterTextChanged(s: Editable?) {
TV?.let {
it.text = textToPrependOnTVAfterTextChanged + s.toString()
}
}
fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) { /* Ignored */ }
fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) { /* Ignored */ }
companion object {
lateinit var TV: TextView
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment