2012-07-16 72 views
2

我的筆記應用程序在屏幕上有兩個片段:筆記片段列表和筆記細節片段,用於在第二個片段中顯示所選筆記,它有一個輸入文本字段(android:inputType =「textMultiLine」)。因爲我想處理事件鍵盤隱藏,所以我可以保存用戶在他/她關閉鍵盤時所做的更改。任何人都可以給我一個線索來完成這項任務嗎?[tablet] [fragment]如何處理隱藏的事件軟鍵盤

+0

默認android的軟鍵盤將爲您做關鍵。點擊這個完成鍵,鍵盤自動隱藏 – Swati 2012-07-16 07:06:54

+0

但我不能使用「完成」鍵,因爲文本字段是多行,而我必須使用「輸入行」鍵代替。你能幫助我嗎? – 2012-07-16 07:25:14

回答

2

下面是我如何處理它在我的注意細節片段:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    this.view = inflater.inflate(R.layout.fragment_note_detail_of_style_searching, container , false); 

    listviewFilter = (ListView) view.findViewById(R.id.listview_filter); 
    noteDetailView = view.findViewById(R.id.note_detail_of_style_view); 
    photoDetailView = view.findViewById(R.id.gv_note_detail_photo); 

    view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 

     @Override 
     public void onGlobalLayout() {     
      int heightDiff = view.getRootView().getHeight() - view.getHeight(); 
      if (heightDiff > 200) { 
       isWatchingKeyboard = true; 
       photoDetailView.setVisibility(View.GONE); 
       return; //must exit now 
      } 

      if(isWatchingKeyboard){ 
       isWatchingKeyboard = false; 
       viewForFocus.requestFocus(); 
       photoDetailView.setVisibility(View.VISIBLE); 
      } 
     } 
    }); 

    return this.view; 
} 
+0

以上這些適合我,但請注意較大設備(如Nexus 5)上的幻數200.值爲217。 – 2014-10-08 11:07:30

0

我就是這麼做的:

private int prevBottom; 

@Override 
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) { 
    view = inflater.inflate(R.layout.fragment_status, container, false); 
    prevBottom = view.getBottom(); 

    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      if (view.getBottom() > prevBottom) { 
       view.requestFocus(); 
      } 
      prevBottom = view.getBottom(); 
     } 
    }); 

    return view; 
} 

,並已將此添加到視圖我在不斷膨脹:

android:focusableInTouchMode="true" 

並且這適用於AndroidManifest.xml中的活動

android:windowSoftInputMode="adjustResize" 
1

我認爲對於鍵盤問題的最佳和通用解決方案,是衡量佈局變化VisibleDisplayFrame:

Rect visibleRect = new Rect(); 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    ViewGroup mMain = (ViewGroup) inflater.inflate(R.layout.select_cover_album, container, false); 

    mMain.getWindowVisibleDisplayFrame(visibleRect); 
    mMain.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { 
      @Override 
      public void onLayoutChange(View v, 
             int left, 
             int top, 
             int right, 
             int bottom, 
             int oldLeft, 
             int oldTop, 
             int oldRight, 
             int oldBottom) { 

       Rect r = new Rect(); 
       mMain.getWindowVisibleDisplayFrame(r); 

       if(r.height() < visibleRect.height()){ 
        //keyboard shown 

       } 
       if(r.height() == visibleRect.height()){ 
        //keyboard hidden 
       } 

      } 
     }); 

     return mMain; 
}