2013-04-17 57 views
32

我想知道如何在WhatsApp和環聊等鍵盤上添加View。在聊天屏幕中,他們在打開的軟鍵盤頂部插入表情符號視圖。如何在WhatsApp等軟鍵盤上繪製視圖?

sample image

有誰知道如何實現這種行爲?

+0

我不不認爲這是WhatsApp發生的事情。我猜,點擊表情按鈕只會解除軟鍵盤並打開笑臉片段。 –

+0

它不排除我的理解鍵盤我認爲這是打開頂部打開鍵盤 –

+0

看到這些http://androidjayavelu.blogspot.in/2011/12/android-imf-is-designed-to-support.html和http ://tutorials-android.blogspot.in/2011/06/create-your-own-custom-keyboard-for.html –

回答

3

據我所知,你可以借鑑其他應用程序,是的。我自己設計了這樣的應用程序。至於在特定的應用程序如鍵盤或任何其他應用程序中繪製,我想,你將不得不定義一個高度正是鍵盤的佈局。所以,這會因設備而異。所以,這是不可能的。

我仍堅持我的觀點,即WhatsApp僅僅按下笑臉按鈕就會解除軟鍵盤並將其稱爲自己的片段。

如果你仍然想追求這一點,下面是你如何繪製一個「窗口」超過其他應用程序。這些應該是它的佈局參數。

params = new WindowManager.LayoutParams(
       WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, 
       WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, 
       PixelFormat.TRANSLUCENT); 

雖然,你的寬度將變爲絕對像素值,因爲你想在活動結束只用鍵盤。

如果我誤解了這個問題,請糾正我。

+0

感謝您的重播,我認爲這種方法比WhatsApp做得更貴一些,因爲它需要一個權限android:name =「android.permission.SYSTEM_ALERT_WINDOW」,WhatsApp沒有這個權限 –

+0

確切的說,所以他們沒有繪製在鍵盤上的笑臉片段:)這只是一個簡單的解僱命令。 –

+0

我不確定,因爲他們解散了那個鍵盤,並且沒有動畫的原生鍵盤進來了,更不用說當表情符號鍵盤打開時活動繼續縮小視圖 –

13

經過長時間的研究和嘗試錯誤之後,我發現了另一種類似於上述Chirag Jain的解決方案,但使用了自定義Dialog。

mDialogKeyboard = new Dialog(this,android.R.style.Theme_NoTitleBar); 
    mDialogKeyboard.setContentView(R.layout.your_custom_layout); 
    mDialogKeyboard.getWindow().setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL); 
    mDialogKeyboard.getWindow().setFlags(WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH); 
    mDialogKeyboard.getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM); 
    mDialogKeyboard.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); 

    WindowManager.LayoutParams lp=mDialogKeyboard.getWindow().getAttributes();  
    lp.width=WindowManager.LayoutParams.MATCH_PARENT; 
    lp.height=mSoftKeyboardHeight; 
    lp.gravity=Gravity.BOTTOM | Gravity.LEFT; 
    lp.dimAmount=0; 

儘管奇拉格耆那教答案似乎是更清潔的事實,我會在這裏發佈本作有一個替代方法。

+0

如何做到這一點,如果customlayout是一個片段,你用這個對話框在你的FragmentActivity中膨脹這個..它會給你二進制充氣錯誤。是不是? – Saty

19

好了,我已經創建了一個樣本鍵盤聊天here ...

在這裏,我使用彈出窗口,顯示彈出窗口和彈出窗口的高度是通過鍵盤

// Initially defining default height of keyboard which is equal to 230 dip 
     final float popUpheight = getResources().getDimension(
       R.dimen.keyboard_height); 
     changeKeyboardHeight((int) popUpheight); 

// Creating a pop window for emoticons keyboard 
    popupWindow = new PopupWindow(popUpView, LayoutParams.MATCH_PARENT, 
      (int) keyboardHeight, false); 

的高度動態計算和高度計算使用此功能:

/** 
* Checking keyboard height and keyboard visibility 
*/ 
int previousHeightDiffrence = 0; 
private void checkKeyboardHeight(final View parentLayout) { 

    parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
      new ViewTreeObserver.OnGlobalLayoutListener() { 

       @Override 
       public void onGlobalLayout() { 

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

        int screenHeight = parentLayout.getRootView() 
          .getHeight(); 
        int heightDifference = screenHeight - (r.bottom); 

        if (previousHeightDiffrence - heightDifference > 50) {       
         popupWindow.dismiss(); 
        } 

        previousHeightDiffrence = heightDifference; 
        if (heightDifference > 100) { 

         isKeyBoardVisible = true; 
         changeKeyboardHeight(heightDifference); 

        } else { 

         isKeyBoardVisible = false; 

        } 

       } 
      }); 

} 

使用所有這些東西我能夠使一個完美的重疊鍵盤....

然後我用viewpager和gridview爲表情圖標充氣彈出窗口。

還有,我用spannable字符串顯示在列表視圖這些表情符號和聊天窗口

+0

我很想看看你的項目是如何工作的,但我無法使用我的Android Studio克隆它。 –

0

什麼我的想法是,他們創造了自己的鍵盤的笑容,和微笑圖標或鍵盤圖標點擊它們都躲在微笑鍵盤並顯示正常的鍵盤。在什麼樣的應用程序情況下有兩種情況1)如果您沒有第一次對editext進行對焦,那麼您將看不到顯示鍵盤按鈕,並且微笑鍵盤的高度與普通鍵盤的高度完全相同,我們將僅獲得鍵盤高度我們的視圖佈局改變後,意味着只有在鍵盤顯示後,這意味着他們正在創建自己的鍵盤..2)如果你的焦點edittext和點擊微笑按鈕,那麼它會顯示選項的顯示鍵盤按鈕請糾正我,如果我不是這個

3

我被困在同樣的問題。我終於設法用軟鍵盤上的PopupWindow來解決它。 我上傳了我的解決方案作爲github上的一個項目:https://github.com/ankushsachdeva/emojicon

+3

如果你能指出你的庫的哪個部分通過粘貼該代碼實際解決了問題,那更好。只是一個鏈接到你的圖書館是毫無意義的! – LostPuppy

0

我最近不得不實現一個視圖,它將位於軟鍵盤上方。 @Chirag Jain的解決方案几乎是正確的,但它不計入屏幕底部的系統按鈕!這將使一些設備,如Nexus 6,該解決方案應該在所有設備上工作的鍵盤高度不正確:

1)創建佈局,包含您的視圖

<RelativeLayout 
     android:id="@+id/keyboard_info_container" 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:layout_alignParentBottom="true" 
     android:background="@color/C12" 
     android:padding="10dp" 
     android:visibility="invisible"> 

      .... 

    </RelativeLayout> 

2)綁定視圖

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

    ButterKnife.bind(this, rootview); 

    checkKeyboardHeight(rootview); 

3)鍵盤檢查和查看頁邊距設置

private void checkKeyboardHeight(final View parentLayout) { 

    parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
      new ViewTreeObserver.OnGlobalLayoutListener() { 

       int previousHeightDiffrence = 0; 
       int systemBarHigh = 999999; 

       @Override 
       public void onGlobalLayout() { 


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

        int screenHeight = parentLayout.getRootView() 
          .getHeight(); 
        int keyboardHeight = screenHeight - (r.bottom); 

        if(systemBarHigh > keyboardHeight) { 
         systemBarHigh = keyboardHeight; 
        } 

        if (keyboardHeight > 250) { 

         int keyboardHightWithoutSystemBar = keyboardHeight - systemBarHigh; 
         // no need to update when the keyboard goes down 
         if (previousHeightDiffrence != keyboardHightWithoutSystemBar) { // if (Math.abs(previousHeightDiffrence - keyboardHeight) > 10) { 
          adjustKeyboard(keyboardHightWithoutSystemBar); 
         } 

         keyboardInfoContainer.setVisibility(View.VISIBLE); 
         isKeyBoardVisible = true; 
         previousHeightDiffrence = keyboardHightWithoutSystemBar; 

        } else { 
         isKeyBoardVisible = false; 
         if (keyboardInfoContainer != null) { 
          keyboardInfoContainer.setVisibility(View.INVISIBLE); 
         } 
        } 
       } 
      }); 
} 

private void adjustKeyboard(int keyboardHeight) { 
    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) keyboardInfoContainer.getLayoutParams(); 
    lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    lp.bottomMargin = keyboardHeight; 
    keyboardInfoContainer.requestLayout(); 
} 
0

@jirkarrr,爲什麼你不加keyboardInfoContainer這樣的:

WindowManager wm = getWindowManager(); 
WindowManager.LayoutParams lps = new WindowManager.LayoutParams(); 
lps.x = 0; lps.y = keyboardHeight; 
wm.addView(keyboardInfoContainer, lps); 

我作爲你的代碼,但它不能顯示出keyboardInfoContainer。

0

我使用彈出把視圖在鍵盤:

public void showPopUpKeyboard() { 
     mIsPopupVisible = true; 
     // Initialize a new instance of LayoutInflater service 
     LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE); 

     // Inflate the custom layout/view 
     View customView = inflater.inflate(R.layout.popup_in_keyboard, null); 


     mScrollView = (ScrollView) customView.findViewById(R.id.keyboard_layout_view); 
     // Initialize a new instance of popup window 
     mPopupWindow = new PopupWindow(
       customView, 
       RelativeLayout.LayoutParams.MATCH_PARENT, 
       RelativeLayout.LayoutParams.MATCH_PARENT 
     ); 


     setSizeForSoftKeyboard(); 

     // Get a reference for the custom view close button 
     Button closeButton = (Button) customView.findViewById(R.id.ib_close); 

     // Set a click listener for the popup window close button 
     closeButton.setOnClickListener((View view) -> { 
       // Dismiss the popup window 
       mIsPopupVisible = false; 
       mPopupWindow.dismiss(); 
     }); 
     mPopupWindow.showAtLocation(mParentLayout, Gravity.CENTER, 0, 0); 

    } 

那我試試就知道了鍵盤的高度:

mParentLayout.getViewTreeObserver().addOnGlobalLayoutListener(() -> { 
       Rect r = new Rect(); 

       mParentLayout.getWindowVisibleDisplayFrame(r); 

       int heightDiff = mParentLayout.getRootView().getHeight() - (r.bottom - r.top); 
       if (heightDiff > 100) { 
        //enter your code here 
        if (mIsPopupVisible) { 
         keepKeyboard(); 
         mIsPopupVisible = false; 
         mPopupWindow.dismiss(); 
        } 
       } else { 
        //enter code for hid 
       } 
     }); 

您可以檢查此tutorialexample in GitHub