键盘在kotlin中打开时如何隐藏底部按钮?

我在我的布局中有一个EditText和一个按钮。

我的按钮是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!!) } 

但躲藏仍然没有成功。

尝试使用这个:

  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 } }