简体   繁体   English

Android:如何按下软键盘上方的按钮

[英]Android: How to push button above soft keyboard

I've got a "save" button which I want to push up together with the soft keyboard.我有一个“保存”按钮,我想将其与软键盘一起向上推。 So when the user clicks an EditText in my layout, then the button has to stay above the keyboard.因此,当用户在我的布局中单击 EditText 时,按钮必须保持在键盘上方。 Now the button becomes hidden underneath the keyboard.现在按钮隐藏在键盘下方。 How do you do this?你怎么做到这一点?

Thanks in advance!提前致谢!

You need to set your keyboard's input mode to adjustResize .您需要将键盘的输入模式设置为adjustResize You can do this adding the following line to your activity's attributes in the manifest:您可以将以下行添加到清单中的活动属性中:

    android:windowSoftInputMode="adjustResize"

Here's an example of the attribute added in the activity:这是在活动中添加的属性的示例:

<activity 
     android:name=".activity.MyActivity"
     android:windowSoftInputMode="adjustResize">
</activity>

Along with Inthathep's answer, you have to add an attribute in the parent viewgroup随着 Inththep 的回答,您必须在父视图组中添加一个属性

android:fitsSystemWindows="true"

to work it as desired.根据需要工作。 ie, in manifest file , for the activity add即,在清单文件中,为活动添加

android:windowSoftInputMode="adjustResize"

and eg.和例如。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:fitsSystemWindows="true" <!-- add this -->
    android:orientation="vertical"
    >
    <EditText
        android:id="@+id/et_assetview_comment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minHeight="80dp"
        android:background="@color/white"
        android:hint="Enter comments"
        />
    <Button
        android:id="@+id/btn_assetview_postcomment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="POST"
        />
</LinearLayout>

Order your layout like this and you will able to put button to above keyboard像这样订购您的布局,您将能够将按钮放在键盘上方

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/button_next"
        android:background="#0ff"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            >

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="250dp"
                android:hint="Hint"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="ABC"
                android:textSize="50sp"
                />
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/button_next"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:layout_margin="10dp"
        android:text="Button Next"
        />

</RelativeLayout>

In android manifest在安卓清单中

<application
        ...
        >
        <activity android:name=".YourActivity"
            android:windowSoftInputMode="adjustResize"
           >
        </activity>
</application>

在此处输入图片说明

Note that, instead of RelativeLayout , you can also use another ViewGroup like LinearLayout with weight, CordinatorLayout , ...请注意,除了RelativeLayout ,您还可以使用另一个ViewGroup如带LinearLayout重的LinearLayoutCordinatorLayout ,...

So this is a pretty old post, but I struggled with the answers provided.所以这是一个很老的帖子,但我对所提供的答案很挣扎。 Both oneavi and Intahep are correct, but let me show you EXACTLY where the android:windowSoftInputMode="adjustResize" goes. oneavi 和 Intahep 都是正确的,但让我向您展示android:windowSoftInputMode="adjustResize"

in Android Manifest在 Android 清单中

    <activity android:name=".DataScreen" />
    <activity android:name=".PauseScreen" />
    <activity android:name=".RouteInfo"
               android:windowSoftInputMode="adjustResize"> <!--This goes in the specific activity with the button -->
    </activity>

最好的方法是隐藏击键,必要时按下键盘上方的按钮

 android:windowSoftInputMode="adjustResize|stateHidden"

Additional point Any of the above including "adjustResize" will work except if your activity is full screen.附加点除非您的活动是全屏的,否则包括"adjustResize"在内的任何上述内容都将起作用。 That was my case.这就是我的情况。 check your activity code to ensure its not in full screen.检查您的活动代码以确保其未全屏显示。

This simple setup scrolls the entire layout up with the keyboard.这个简单的设置用键盘向上滚动整个布局。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <ImageView
            android:id="@+id/image_iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/recycler_view"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:src="@drawable/image" />

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/button"
            android:layout_centerHorizontal="true" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:text="Button" />

</RelativeLayout>

Key points:关键点:

  • Nothing added to the manifest.清单中没有添加任何内容。
  • Image must be "wrap_content" (not fixed size) to allow android to resize it as needed.图像必须是“wrap_content”(非固定大小)以允许 android 根据需要调整其大小。
  • All views must be linked as shown above.所有视图都必须如上所示链接。 The bottom view layout_alignParentBottom="true", the top view layout_alignParentTop="true", and all intermediate views layout_above="@id/view_below".底视图 layout_alignParentBottom="true",顶视图 layout_alignParentTop="true",以及所有中间视图 layout_above="@id/view_below"。
  • The layout_below="@id/view_above" attribute does not work for some reason. layout_below="@id/view_above" 属性由于某种原因不起作用。

In AndroidX:在 AndroidX 中:

Use CoordinatorLayout for main parent layout and add a NestedScrollView for your content and add your layout or button in child of CoordinatorLayout to push button above soft keyboardCoordinatorLayout用于主父布局并为您的内容添加一个NestedScrollView并将您的布局或按钮添加到CoordinatorLayout的子级以按下软键盘上方的按钮

<androidx.coordinatorlayout.widget.CoordinatorLayout  
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >
    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         android:isScrollContainer="true"  >
        .......
    </androidx.core.widget.NestedScrollView>
    <com.google.android.material.button.MaterialButton
        android:id="@+id/send_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="@string/login" />

photo:照片:

https://snipboard.io/n45tbx.jpg

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

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