【问题标题】:FAB responded when edit text click, FAB comes up with keyboardFAB在编辑文本点击时响应,FAB拿出键盘
【发布时间】:2017-06-29 17:50:42
【问题描述】:

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

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

我已经在主 Activity 中实现了 FAB,所以我无法为 EditText 焦点监听器隐藏键盘监听器,任何人都可以与我分享解决方案。

【问题讨论】:

    标签: android floating-action-button


    【解决方案1】:

    没有直接的方法可以知道软键盘何时打开,但是您可以执行以下操作:

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

    【讨论】:

    • 使用视图寻呼机我实现了多个片段如何使用这个代码你能告诉我。我试过了,但是 fab 突然打开和关闭,然后 fab 处于 mainactivity 它显示所有片段
    【解决方案2】:

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

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

    此问题中列出的更详细示例:SoftKeyboard open and close listener in an activity in Android?

    【讨论】:

      猜你喜欢
      • 2018-06-28
      • 2016-02-18
      • 1970-01-01
      • 2015-06-03
      • 2018-01-06
      • 2019-05-19
      • 2016-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多