2017-02-11 5 views
0

私はGoogleの新しいデザインサポートライブラリのFABを使用しています。私は長い形式の画面とFABを持っています。私はソフトキーボードが開くとFABが消えたがっています。ソフトキーボードが開いていることを検出する方法が見つかりません。他のオプションはありますかFABはテキストを編集するときに反応し、FABはキーボードで表示されます

Fragmentに含まれているすべてのものとしてリスナーをEditTextに設定することはできません。オンフォーカス変更リスナーは別のFragmentで利用できません。

私はメインActivityのFABを実装していますので、キーボードのリスナーを隠すことができませんEditTextフォーカスリスナー誰でも私とソリューションの共有をしてください。

答えて

2

ソフトキーボードが開いたときに、しかし、あなたは、次の操作を行うことができます知っている直接的な方法はありません。

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

おかげゼッド。私はこの問題をurコードで解決しました...... –

+0

ビューページャを使用して私はmutilpleフラグメントを実装しました。このコードをどのように使用するかは、私に教えてください。私はしようとしたが、ファブが開かれ、突然閉じた後、ファブは主な活動にあった –

0

あなたは、キーボードの開閉のために聞くことができました。この質問に記載されている

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?

関連する問題