简体   繁体   English

Android底部操作栏,在使用软键盘时不会隐藏字段

[英]Android bottom action bar that doesn't hide fields when using soft keyboard

I'm disabling the normal top action bar by using: 我通过使用以下命令禁用了正常的顶部操作栏:

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

& I want to use a bottom action bar to have the done/cancel actions like this calendar app: 我想使用底部操作栏来执行此日历应用程序的已完成/取消操作: 在此处输入图片说明

but when I try to write something to the editTexts available in the scrollView, the bottom action bar hides the fields, & I want it to be visible like also the calendar app below: 但是,当我尝试将一些内容写入scrollView中可用的editTexts时,底部的操作栏将隐藏这些字段,并且我希望它像下面的日历应用程序一样可见: 在此处输入图片说明

So, how can I achieve similar behavior? 那么,如何实现类似的行为? (so the bottom action bar won't hide any field when opening the soft keyboard), (因此,打开软键盘时,底部操作栏不会隐藏任何字段),

I'm using code like this: 我正在使用这样的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="true"
android:orientation="vertical"
android:weightSum="1">

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:fillViewport="false"
    android:id="@+id/formScrollView">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <!-- all form fields goes here -->
    </LinearLayout>

</ScrollView>

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:padding="@dimen/done_button_padding"
    android:id="@+id/happeningDoneLayout">

    <Button
        android:id="@+id/doneButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="@string/done"
        android:layout_alignParentBottom="true"/>
    <Button
        android:id="@+id/cancelButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="@string/cancel"
        android:layout_alignParentBottom="true"/>
</FrameLayout>

</RelativeLayout>

Simplest thing to do is to prevent your layout from being resized when the virtual keyboard comes up: 最简单的操作是防止虚拟键盘出现时调整布局大小:

    <activity
        android:name=".ShareFromDriveActivity_"
        android:hardwareAccelerated="true"
        android:label="@string/titleSharingCalendar"
        android:launchMode="standard"
        android:parentActivityName=".AppWidgetConfigure_"
        android:screenOrientation="sensor"
        android:theme="@style/Theme.Materialamberpurple"
        android:windowSoftInputMode="stateHidden|adjustPan" >
        <intent-filter>
            <action android:name="de.kashban.android.picturecalendar.INTENT_ACTION_SHARE_FROM_DRIVE" />
        </intent-filter>
    </activity>

The important line is android:windowSoftInputMode="stateHidden|adjustPan" . 重要的一行是android:windowSoftInputMode =“ stateHidden | adjustPan” stateHidden ensures the keyboard is not opened up when starting the activity even if an EditText has focus. stateHidden可以确保即使启动EditText时也不会在启动活动时打开键盘。

adjustPan is what you are looking for: The Layout will no longer be resized (including your lower buttons), but the keyboad will overlay the layout. AdjustPan是您要寻找的:布局将不再被调整大小(包括您的下部按钮),但是键盘将覆盖该布局。 It is still possible to scroll them into the visible part, but when the keyboard comes up, they are not visible. 仍然可以将它们滚动到可见部分,但是当键盘抬起时,它们将不可见。

Source: Android Guides 资料来源: Android指南

Perhaps this setting alone will help your case. 也许仅此设置将对您的情况有所帮助。

If that's not enough and you require the Buttons to be really gone, try using this: 如果这还不够,并且您要求按钮真正消失,请尝试使用以下命令:

// Detect soft keyboard visibility changes
final SoftKeyboardStateHelper softKeyboardStateHelper = 
        new SoftKeyboardStateHelper(lyt_share_from_drive_main);
softKeyboardStateHelper.addSoftKeyboardStateListener(this);

SoftKeyboardStateHelper is a class from Artem Zinnatullin to detect state changes of the Softkeyboard: SoftKeyboardStateHelper是Artem Zinnatullin的一个类,用于检测软键盘的状态变化:

/**
 * 
 */
package de.kashban.android.picturecalendar.util.local;

/**
 * @author Artem Zinnatullin
 * http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android/9108219#9108219
 * Usage: final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(findViewById(R.id.activity_main_layout);
 *        softKeyboardStateHelper.addSoftKeyboardStateListener(...);
 */
import android.graphics.Rect;
import android.view.View;
import android.view.ViewTreeObserver;

import java.util.LinkedList;
import java.util.List;

public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener {

    public interface SoftKeyboardStateListener {
        void onSoftKeyboardOpened(int keyboardHeightInPx);
        void onSoftKeyboardClosed();
    }

    private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>();
    private final View activityRootView;
    private int        lastSoftKeyboardHeightInPx;
    private boolean    isSoftKeyboardOpened;

    public SoftKeyboardStateHelper(View activityRootView) {
        this(activityRootView, false);
    }

    public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) {
        this.activityRootView     = activityRootView;
        this.isSoftKeyboardOpened = isSoftKeyboardOpened;
        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
    }

    @Override
    public void onGlobalLayout() {
        final Rect r = new Rect();
        //r will be populated with the coordinates of your view that area still visible.
        activityRootView.getWindowVisibleDisplayFrame(r);

        final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
        if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
            isSoftKeyboardOpened = true;
            notifyOnSoftKeyboardOpened(heightDiff);
        } else if (isSoftKeyboardOpened && heightDiff < 100) {
            isSoftKeyboardOpened = false;
            notifyOnSoftKeyboardClosed();
        }
    }

    public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
        this.isSoftKeyboardOpened = isSoftKeyboardOpened;
    }

    public boolean isSoftKeyboardOpened() {
        return isSoftKeyboardOpened;
    }

    /**
     * Default value is zero (0)
     * @return last saved keyboard height in px
     */
    public int getLastSoftKeyboardHeightInPx() {
        return lastSoftKeyboardHeightInPx;
    }

    public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
        listeners.add(listener);
    }

    public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
        listeners.remove(listener);
    }

    private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {
        this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;

        for (SoftKeyboardStateListener listener : listeners) {
            if (listener != null) {
                listener.onSoftKeyboardOpened(keyboardHeightInPx);
            }
        }
    }

    private void notifyOnSoftKeyboardClosed() {
        for (SoftKeyboardStateListener listener : listeners) {
            if (listener != null) {
                listener.onSoftKeyboardClosed();
            }
        }
    }
}

In your activity implement the Interface SoftKeyboardStateListener and override these methods: 在您的活动中实现Interface SoftKeyboardStateListener并覆盖以下方法:

@Override
public void onSoftKeyboardOpened(int keyboardHeightInPx) {
    if (D.DEBUG_APP) Log.d(TAG, "onSoftKeyboardOpened() called with keyboard height " + keyboardHeightInPx);
    rdgVisibility.setVisibility(View.GONE);
    if (tvPermissionLabel != null)
        tvPermissionLabel.setVisibility(View.GONE);
    lyt_ShareDriveOkCancel.setVisibility(View.GONE);
    cbShareWithDev.setVisibility(View.GONE);

}

@Override
public void onSoftKeyboardClosed() {
    if (D.DEBUG_APP) Log.d(TAG, "onSoftKeyboardClosed() called.");
    rdgVisibility.setVisibility(View.VISIBLE);
    if (tvPermissionLabel != null)
        tvPermissionLabel.setVisibility(View.VISIBLE);
    lyt_ShareDriveOkCancel.setVisibility(View.VISIBLE);
    cbShareWithDev.setVisibility(View.VISIBLE);
}

In these two methods change the Visibility of your lower Buttons accordingly. 在这两种方法中,相应地更改下部按钮的可见性。 Done. 完成。

Here's how it looks in my app: 这是我的应用程序中的外观:

完整对话框截图

Keyboard is closed, full layout visible 键盘已关闭,可见完整布局

隐藏的大多数控件截图

Keyboard is open, all controls but EditText gone. 键盘已打开,除EditText之外的所有控件均已消失。 Reason is that the EditText could span several lines and on small screens it was too cluttered with the full layout in place. 原因是EditText可能跨越多行,并且在小屏幕上,整个布局太混乱了。

Android Keyboard hides EditText Android键盘隐藏EditText

android:windowSoftInputMode="adjustPan|adjustResize" 机器人:windowSoftInputMode = “adjustPan | adjustResize”

That should give you the effect your looking for. 那应该给您您寻找的作用。 Put that in the manifest of the relevant activity. 将其放在相关活动的清单中。

To make sure the bottom action bar will not hide any other controls, the ScrollView and the bar can be stacked in a vertical linear layout. 为确保底部操作栏不会隐藏任何其他控件,可以将ScrollView和该栏堆叠为垂直线性布局。 This allows the ScrollView to shrink/expand with the focused control visible when the keyboard appears/disappears, while keeping the bottom action bar always visible at the bottom of the screen below the ScrollView. 当键盘出现/消失时,这允许ScrollView缩小/展开并使焦点控件可见,同时使底部动作栏始终在ScrollView下方的屏幕底部始终可见。

adjustPan should not be used with this solution. AdjustPan不应与此解决方案一起使用。

The weights are distributed such that the ScrollView is the part that would change its height dynamically. 权重分布使得ScrollView是可以动态更改其高度的部分。

Here's a minimized sample of the code: 这是最小化的代码示例:

<?xml version="1.0" encoding="utf-8"?>

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">


        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/formScrollView"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            ... >

                ...

        </ScrollView>

        <FrameLayout
            android:id="@+id/bottomBar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            ... >

                ...

        </FrameLayout>

    </LinearLayout>

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

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