Android 当编辑文本点击时,FAB响应,FAB拿出键盘

Android 当编辑文本点击时,FAB响应,FAB拿出键盘,android,floating-action-button,Android,Floating Action Button,我正在使用谷歌新设计支持库中的FAB。我有一个长形屏幕和一个晶圆厂。我希望当软键盘打开时,晶圆厂消失。找不到检测软键盘打开的方法。还有其他选择吗 我无法将侦听器设置为EditText,因为所有侦听器都包含在不同的片段中,并且焦点更改侦听器在另一个片段中不可用 我在main活动中实现了FAB,因此我无法为EditTextfocus listener隐藏键盘侦听器。任何人都可以与我共享解决方案。无法直接知道软键盘何时打开,但您可以执行以下操作: contentView.getViewTreeObse

我正在使用谷歌新设计支持库中的FAB。我有一个长形屏幕和一个晶圆厂。我希望当软键盘打开时,晶圆厂消失。找不到检测软键盘打开的方法。还有其他选择吗

我无法将侦听器设置为
EditText
,因为所有侦听器都包含在不同的
片段中,并且焦点更改侦听器在另一个
片段中不可用


我在main
活动中实现了FAB,因此我无法为
EditText
focus listener隐藏键盘侦听器。任何人都可以与我共享解决方案。

无法直接知道软键盘何时打开,但您可以执行以下操作:

contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {

    Rect r = new Rect();
    contentView.getWindowVisibleDisplayFrame(r);
    int screenHeight = contentView.getRootView().getHeight();

    // r.bottom is the position above soft keypad or device button.
    // if keypad is shown, the r.bottom is smaller than that before.
    int keypadHeight = screenHeight - r.bottom;

    if (keypadHeight > screenHeight * 0.15) {
        // keyboard is opened
        // Hide your FAB here
    }
    else {
        // keyboard is closed
    }
}
});

无法直接知道软键盘何时打开,但您可以执行以下操作:

contentView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {

    Rect r = new Rect();
    contentView.getWindowVisibleDisplayFrame(r);
    int screenHeight = contentView.getRootView().getHeight();

    // r.bottom is the position above soft keypad or device button.
    // if keypad is shown, the r.bottom is smaller than that before.
    int keypadHeight = screenHeight - r.bottom;

    if (keypadHeight > screenHeight * 0.15) {
        // keyboard is opened
        // Hide your FAB here
    }
    else {
        // keyboard is closed
    }
}
});

你可以听键盘的打开和关闭

public class BaseActivity extends Activity {
private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int heightDiff = rootLayout.getRootView().getHeight() - rootLayout.getHeight();
        int contentViewTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();

        LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(BaseActivity.this);

        if(heightDiff <= contentViewTop){
            onHideKeyboard();

            Intent intent = new Intent("KeyboardWillHide");
            broadcastManager.sendBroadcast(intent);
        } else {
            int keyboardHeight = heightDiff - contentViewTop;
            onShowKeyboard(keyboardHeight);

            Intent intent = new Intent("KeyboardWillShow");
            intent.putExtra("KeyboardHeight", keyboardHeight);
            broadcastManager.sendBroadcast(intent);
        }
    }
};

private boolean keyboardListenersAttached = false;
private ViewGroup rootLayout;

protected void onShowKeyboard(int keyboardHeight) {}
protected void onHideKeyboard() {}

protected void attachKeyboardListeners() {
    if (keyboardListenersAttached) {
        return;
    }

    rootLayout = (ViewGroup) findViewById(R.id.rootLayout);
    rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);

    keyboardListenersAttached = true;
}

@Override
protected void onDestroy() {
    super.onDestroy();

    if (keyboardListenersAttached) {
        rootLayout.getViewTreeObserver().removeGlobalOnLayoutListener(keyboardLayoutListener);
    }
}
}
公共类BaseActivity扩展活动{
私有ViewTreeObserver.OnGlobalLayoutListener键盘布局Listener=新ViewTreeObserver.OnGlobalLayoutListener(){
@凌驾
公共图书馆{
int-heightDiff=rootLayout.getRootView().getHeight()-rootLayout.getHeight();
int contentViewTop=getWindow().findViewById(Window.ID\u ANDROID\u CONTENT.getTop();
LocalBroadcastManager-broadcastManager=LocalBroadcastManager.getInstance(BaseActivity.this);

如果(heightDiff,您可以听到键盘的打开和关闭

public class BaseActivity extends Activity {
private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        int heightDiff = rootLayout.getRootView().getHeight() - rootLayout.getHeight();
        int contentViewTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();

        LocalBroadcastManager broadcastManager = LocalBroadcastManager.getInstance(BaseActivity.this);

        if(heightDiff <= contentViewTop){
            onHideKeyboard();

            Intent intent = new Intent("KeyboardWillHide");
            broadcastManager.sendBroadcast(intent);
        } else {
            int keyboardHeight = heightDiff - contentViewTop;
            onShowKeyboard(keyboardHeight);

            Intent intent = new Intent("KeyboardWillShow");
            intent.putExtra("KeyboardHeight", keyboardHeight);
            broadcastManager.sendBroadcast(intent);
        }
    }
};

private boolean keyboardListenersAttached = false;
private ViewGroup rootLayout;

protected void onShowKeyboard(int keyboardHeight) {}
protected void onHideKeyboard() {}

protected void attachKeyboardListeners() {
    if (keyboardListenersAttached) {
        return;
    }

    rootLayout = (ViewGroup) findViewById(R.id.rootLayout);
    rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);

    keyboardListenersAttached = true;
}

@Override
protected void onDestroy() {
    super.onDestroy();

    if (keyboardListenersAttached) {
        rootLayout.getViewTreeObserver().removeGlobalOnLayoutListener(keyboardLayoutListener);
    }
}
}
公共类BaseActivity扩展活动{
私有ViewTreeObserver.OnGlobalLayoutListener键盘布局Listener=新ViewTreeObserver.OnGlobalLayoutListener(){
@凌驾
公共图书馆{
int-heightDiff=rootLayout.getRootView().getHeight()-rootLayout.getHeight();
int contentViewTop=getWindow().findViewById(Window.ID\u ANDROID\u CONTENT.getTop();
LocalBroadcastManager-broadcastManager=LocalBroadcastManager.getInstance(BaseActivity.this);

如果(heightDiff使用查看寻呼机我实现了多个片段如何使用此代码你能告诉我吗?我尝试过但突然打开和关闭了fab然后fab处于主要活动中它显示了所有片段使用查看寻呼机我实现了多个片段如何使用此代码你能告诉我吗?我尝试过但fab突然打开和关闭然后fab处于主要活动状态,显示了所有碎片