Forked from MizzleDK/Kotlin interview code test.kt
Last active
July 21, 2022 13:44
-
-
Save ZahidRasheed/2d225e42bf4785016c56da11727126c0 to your computer and use it in GitHub Desktop.
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
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? { | |
// Inflate our awesome layout | |
return View.inflate(activity!!, R.id.awesome_layout, null) | |
} | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
val textView = view.findViewById<TextView>(R.layout.textView1) | |
with (textView) { | |
// 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?.apply { | |
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