我的布局中有一个EditText和一个Button。
我的按钮是parentbottom对齐的。因此,每当我尝试在edittext中键入内容时,按钮就会出现。
所以要隐藏按钮我试过它: `
fun hideButton(editText: EditText, button: Button) {
editText.viewTreeObserver.addOnGlobalLayoutListener {
val r = Rect()
editText.getWindowVisibleDisplayFrame(r)
val screenHeight = editText.rootView.height
val keypadHeight = screenHeight - r.bottom
if (keypadHeight > screenHeight * 0.15) {
// keyboard is open
button.visibility = View.GONE
} else {
// keyboard is closed
button.visibility = View.VISIBLE
}
}
}
并将此函数称为:
edText?.setOnClickListener { hideButton(edName!!, btnSave!!) }
但是仍然没有成功隐藏。
答案 0 :(得分:4)
尝试使用:
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if(b){
//Hide button here
}else{
//Show button here
}
}
});
希望这有帮助。
编辑:Kotlin版本 -
editText.onFocusChangeListener = View.OnFocusChangeListener { view, b ->
if (b) {
//Hide Button
} else {
//Show Button
}
}