-
-
Save MizzleDK/508bec5156110fcd7ab07c7946bc3e7c to your computer and use it in GitHub Desktop.
This file contains 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
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