简体   繁体   English

我想在软键盘上方创建一个视图

[英]I want to create a view above the soft keyboard

I am developing a Android Soft Keyboard. 我正在开发一个Android软键盘。 I want to create a layout above the Soft Keyboard. 我想在软键盘上方创建一个布局。 Whenever keyboard show on the screen the layout must visible. 只要键盘在屏幕上显示,布局就必须可见。

You can easily understand my idea by seeing this image. 通过查看此图像,您可以轻松理解我的想法。

在此处输入图片说明

If you're writing the keyboard, its easy. 如果您正在编写键盘,则非常简单。 Just override onCreateInputView to return the view you want. 只需重写onCreateInputView即可返回所需的视图。 This can easily be a linear layout with your extra views and the keyboard itself in it. 这可以很容易地成为线性布局,并带有额外的视图和键盘本身。

The bigger problem I see is you have an EditText in there. 我看到的最大问题是您那里有一个EditText。 That's not going to work. 那是行不通的。 Tapping on the EditText is going to break the InputConnection to the actual app and cause... unknown weird behavior. 轻按EditText将断开InputConnection与实际应用的连接,并导致...未知的怪异行为。 I'm not even sure if the behavior will be defined across different OS versions. 我什至不确定行为是否会在不同的OS版本中定义。 It may cause the keyboard to immediately hide. 这可能会导致键盘立即隐藏。 It may cause the keyboard to just stop working at all. 这可能会导致键盘完全停止工作。 The OS isn't meant for that. 该操作系统并不适合于此。

As mentioned by Jawad Ahmend in the comments, it's possible to attach the layout to the top of the keyboard by attaching it to the parent bottom using ConstraintLayout . 就像Jawad Ahmend在评论中提到的那样,可以使用ConstraintLayout将布局附加到键盘的底部,从而将布局附加到键盘的顶部。 You'd essentially need to do the following steps: 您基本上需要执行以下步骤:

  1. Set the windowSoftInputMode as adjustResize for your activity in the manifest. adjustResize中的活动将windowSoftInputMode设置为adjustResize
     <activity android:name=".MainActivity" android:label="@string/app_name" android:windowSoftInputMode="adjustResize" android:theme="@style/AppTheme.NoActionBar"> 
  2. Set your layout visibility to gone and add a layout constraint attaching it's bottom to the bottom of the parent. 将布局visibility设置为“消失”,并在其底部和父级底部之间添加一个布局约束。
     <LinearLayout android:id="@+id/layout_B" app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="gone"> 
  3. Next, you need to monitor the soft keyboard state. 接下来,您需要监视软键盘状态。 To keep this short, let's just use the KeyboardVisibilityEvent library . 为了简短起见,我们只使用KeyboardVisibilityEvent Add the following line to your app's build.gradle and sync it. 将以下行添加到您应用的build.gradle中并进行同步。
     implementation 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:2.3.0' 
  4. Add a keyboard listener in your activity to set your layout_B to become visible when the keyboard opens and you're done. 在您的活动中添加键盘侦听器,以将layout_B设置为在键盘打开并完成后可见。
     KeyboardVisibilityEvent.setEventListener(this) { keyboardIsOpen -> layout_B.visibility = if (keyboardIsOpen) { View.VISIBLE } else { View.GONE } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM