我想知道如何在WhatsApp和Hangout等键盘上添加View
。在聊天屏幕中,他们在打开的软键盘顶部插入表情符号视图。
有谁知道如何实现这种行为?
答案 0 :(得分: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字符串在listview和聊天窗口中显示这些表情符号
答案 1 :(得分:13)
经过大量的研究和尝试与错误之后,我发现了另一种类似于 Chirag Jain 的解决方案,但使用的是自定义对话框。
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;
尽管 Chirag Jain 的答案看起来更干净,但我会在这里发布这个替代方法。
答案 2 :(得分: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);
虽然您的宽度将更改为绝对像素值,因为您希望活动仅在键盘上方。
如果我误解了这个问题,请纠正我。
答案 3 :(得分:3)
我遇到了同样的问题。我终于通过软键盘上的PopupWindow解决了这个问题。 我已将我的解决方案作为项目上传到github:https://github.com/ankushsachdeva/emojicon
答案 4 :(得分:0)
我的想法是他们创造了自己的微笑键盘,点击微笑图标或键盘图标,他们隐藏微笑键盘并显示正常的键盘。什么情况下有两种情况应用案例1)如果您没有第一次关注editext然后您看不到显示键盘按钮,并且微笑键盘的高度与普通键盘完全相同,我们将仅获得键盘高度在我们的视图布局更改后,仅表示键盘显示后,表示他们正在创建自己的键盘.2)如果您关注编辑文本并单击微笑按钮,则会显示显示键盘按钮的选项请更正我如果我不对这个
答案 5 :(得分: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();
}
答案 6 :(得分:0)
@jirkarrr,为什么不像这样添加keyboardInfoContainer:
WindowManager wm = getWindowManager();
WindowManager.LayoutParams lps = new WindowManager.LayoutParams();
lps.x = 0; lps.y = keyboardHeight;
wm.addView(keyboardInfoContainer, lps);
我做你的代码,但它无法显示出keyboardInfoContainer。
答案 7 :(得分: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
}
});
您可以查看此tutorial和此example in GitHub