Kotlin Android Login Screen with Inheritance and Extension Functions
This tutorial demonstrates how to build an Android login interface using Kotlin, covering class inheritance, nullable variable declarations, lambda listeners, network requests, and an extension function that adds a toast method to all Activity subclasses, with full source code examples.
This article continues a series on developing Android applications with Kotlin. It first suggests reading the previous two articles on Kotlin basics and Android development before proceeding.
The main example is a complete Kotlin login screen that includes UI components, click listeners, input validation, and a network request to an authentication API.
/**
* 主界面
* @author kymjs (https://www.kymjs.com/)
*/
public class MainActivity : KJActivity() {
var mImgHead: RoundImageView? = null
var mEtUserName: AppCompatEditText? = null
var mImgUserDel: ImageView? = null
var mEtPassWord: AppCompatEditText? = null
var mImgPwdDel: ImageView? = null
var mBtnLogin: AppCompatButton? = null
val kjh: KJHttp = KJHttp()
override fun setRootView() {
setContentView(R.layout.activity_login)
}
override fun initWidget() {
mImgHead = bindView(R.id.login_img_avatar)
mEtUserName = bindView(R.id.login_et_email)
mImgUserDel = bindView(R.id.login_img_email_delete)
mEtPassWord = bindView(R.id.login_et_password)
mImgPwdDel = bindView(R.id.login_img_pwd_delete)
mBtnLogin = bindView(R.id.login_btn)
mBtnLogin?.setOnClickListener { v: View ->
doLogin()
}
mImgUserDel?.setOnClickListener { v: View ->
mEtUserName!!.setText(null)
}
mImgPwdDel?.setOnClickListener { v: View ->
mEtUserName!!.setText(null)
mEtPassWord!!.setText(null)
}
}
fun doLogin() {
val account: String? = mEtUserName!!.getText().toString();
val pwd: String? = mEtPassWord!!.getText().toString();
if (StringUtils.isEmpty(account) || StringUtils.isEmpty(pwd)) {
toast("用户名或密码不能为空")
return
}
val params: HttpParams = HttpParams()
params.put("username", account)
params.put("pwd", pwd)
kjh.post("http://www.oschina.net/action/api/login_validate", params, CallBack())
}
public class CallBack : HttpCallBack() {
override fun onSuccess(s: String) {
KJLoger.debug("网络请求成功,$s")
}
override fun onFailure(code: Int, msg: String) {
KJLoger.debug("网络请求失败,$msg")
}
}
fun Activity.toast(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, message, duration).show()
}
}The layout XML file is omitted for brevity.
Class Inheritance MainActivity inherits from KJActivity (which is part of KJFrameForAndroid). In Kotlin, inheritance is expressed with a colon after the class name.
Variable Declarations Six mutable variables are declared with var and one immutable variable with val . A question mark after the type (e.g., RoundImageView? ) marks the variable as nullable. The double‑exclamation mark ( !! ) forces a non‑null assertion, while the safe‑call operator ( ?. ) executes the following call only if the variable is not null.
mImgUserDel?.setOnClickListener { v: View ->
mEtUserName!!.setText(null)
}Adding Methods to a Specified Class Kotlin allows extension functions similar to Objective‑C categories. The example adds a toast extension function to the Activity class, enabling any Activity subclass to call toast() directly.
Related series: Series | Using Kotlin to Develop Android Series | Kotlin Basic Syntax
Images illustrating the UI are included in the original article.
Hujiang Technology
We focus on the real-world challenges developers face, delivering authentic, practical content and a direct platform for technical networking among developers.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.