android - Kotlin - 如何知道editText是否失去焦点
问题描述
在我的应用程序中,我需要能够捕捉到editText
失去焦点的情况,我已经找到了如何在 java 中做到这一点,但我不太能够在 kotlin 中找到如何做到这一点。
我找到的 Java 答案是这个:
EditText txtEdit = (EditText) findViewById(R.id.edittxt);
txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
// code to execute when EditText loses focus
}
}
});
谁能帮我把它转换成 kotlin?
解决方案
这边走
// Class level
lateinit var editText: EditText
// Method onCreate/onCreateView
editText = findViewById(R.id.whatever)
editText.setOnFocusChangeListener { view, hasFocus ->
if(!hasFocus) {
}
}
推荐阅读
- prolog - 将单词更改为正确的单词
- java - 经过多次旋转后在给定索引处查找元素
- javascript - 获取表中每列的最大宽度 Javascript / Jquery
- powershell - 通过使用 PowerShell 的 Azure AD 身份验证从 Azure Functions 使用 Azure REST API
- r - 从 Redshift 提取数据时,renderDataTable 不起作用
- python - Python tkinter 2 类按钮,只有 1 个可见
- scikit-learn - GridSearchCV 的 TimeSeriesSplit 在 n_splits>2 时失败
- python - 链接两个 python 字典
- android - Android:SQLite 到 csv 导出
- android - CollapsingToolbarLayout 平滑滚动问题