2014-10-31 79 views
1

我在myapp中遇到了一個非常簡單的問題。我有一個自定義對話框,其中有EditText和softkeyboard opensup我想在對話框佈局顯示標題/另一個佈局(見圖片與三個文字)。如果他點擊完成。 hidethesoft鍵盤以及標題。在edittext中顯示鍵盤時彈出headerview /佈局

 ettagmsg = (EditText) dialog.findViewById(R.id.etFlyTagName); 

彈出頭

 LinearLayout layheader = (LinearLayout)findViewById(R.layout.header_buttons); 

enter image description here

+0

不知道我理解你的問題,你想檢測當鍵盤顯示爲N隱藏,這樣你們可以表演n隱藏看法? – Budius 2014-10-31 14:22:44

+0

我希望顯示每次鍵盤顯示時有三個textview的佈局。 – 2014-10-31 14:31:15

回答

1

你可能想添加這個監聽器!

ettagmsg.setOnFocusChangeListener(new View.OnFocusChangeListener(){ 

     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      if(v.hasFocus()){ 
      layheader.setVisibility(View.VISIBLE); 
      }else{ 
      layheader.setVisibility(View.GONE); 
      //hide soft input here 
      } 
     } 
} 

希望我有用!

0

鍵盤在Android上很煩人。你可以自由地使用我之前完成的這個類:

你用Listener(你的對話框)實例化它,並且在onStart/onStop或類似的回調期間附加並從視圖中分離它。請記住,您要將其附加到對話框視圖。

也,你可能需要調整DP_KEYBOARD_THRESHOLD

public class KeyboardObserver implements ViewTreeObserver.OnGlobalLayoutListener, ViewTreeObserver.OnPreDrawListener { 

    private static final int DP_KEYBOARD_THRESHOLD = 60; 
    private int keyboardThreshold; 

    private int currentHeight; 
    private View view; 
    private final KeyboardListener listener; 
    private boolean isKeyboardShown = false; 

    public KeyboardObserver(KeyboardListener listener) { 
     this.listener = listener; 
    } 

    public void attachToView(View view) { 

     keyboardThreshold = (int) TypedValue.applyDimension(
     TypedValue.COMPLEX_UNIT_DIP, DP_KEYBOARD_THRESHOLD, view.getResources().getDisplayMetrics()); 

     this.view = view; 
     currentHeight = view.getHeight(); 
     view.getViewTreeObserver().addOnGlobalLayoutListener(this); 

     if (currentHeight <= 0) { 
     view.getViewTreeObserver().addOnPreDrawListener(this); 
     } 

    } 

    public void detachFromView() { 
     if (view != null) view.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
    } 

    @Override 
    public void onGlobalLayout() { 
     int newHeight = view.getHeight(); 
     if (currentHeight > 0) { 
     int diff = newHeight - currentHeight; 
     if (diff < -keyboardThreshold) { 
      Log.d(this, "onGlobalLayout. keyboard is show. height diff = " + -diff); 
      // keyboard is show 
      isKeyboardShown = true; 
      if (listener != null) 
       listener.onKeyboardShow(-diff); 
     } else if (diff > keyboardThreshold) { 
      Log.d(this, "onGlobalLayout.keyboard is hide. height diff = " + diff); 
      // keyboard is hide 
      isKeyboardShown = false; 
      if (listener != null) 
       listener.onKeyboardHide(diff); 
     } else { 
      Log.v(this, "onGlobalLayout. height diff = " + diff); 
     } 
     } 
     currentHeight = newHeight; 
    } 

    public boolean isKeyboardShown() { 
     return isKeyboardShown; 
    } 

    @Override 
    public boolean onPreDraw() { 
     currentHeight = view.getHeight(); 
     view.getViewTreeObserver().removeOnPreDrawListener(this); 
     return true; 
    } 

    public interface KeyboardListener { 
     public void onKeyboardShow(int height); 

     public void onKeyboardHide(int height); 
    } 
}