您的位置:首页 > 移动开发 > Android开发

Android:完美解决 — 表情键盘与输入法键盘显示冲突方案

2015-11-26 14:51 603 查看
在做发帖功能的时候,一般都会用到表情,当在输入法键盘显示的情况下,点击显示表情键盘的按钮,如果只是简单的setVisibility,同时隐藏输入法,这时表情键盘会被往上弹出,这个效果就让使用体验不太友好。

我的解决方案是:在输入法键盘完全隐藏后,再将表情键盘显示。

1、在xml布局中的最底部加一个View,高度为0,用来在代码中判断该view的y坐标是否大于或等于屏幕的高度,如果是,这表明输入法完全隐藏;

2、通过mainLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener 来监听整个布局的视图高度,判断输入法是否完全隐藏就在这里面操作。

下面代码不太完整,只是用来大概展示思路:

xml布局

<?xml version="1.0" encoding="utf-8"?>
<com.hmy.view.ResizeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical">

<FrameLayout
android:id="@+id/content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/head_layout">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<View
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_alignParentBottom="true" />

<!-- 编辑框 -->
<EditText
android:id="@+id/titleEditText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1.5dp"></EditText>

</RelativeLayout>

<LinearLayout
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#F1F1F1"
android:orientation="vertical">

<!-- 底部功能条,表情按钮在这里面 -->
<include layout="@layout/view_toolbtn" />

<!-- 表情键盘,默认为gone -->
<include layout="@layout/view_emoji" />

<!-- 这个View就是用来标记输入法位置的 -->
<View
android:id="@+id/keyboard_place_view"
android:layout_width="match_parent"
android:layout_height="0dp" />

</LinearLayout>
</FrameLayout>

</com.hmy.view.ResizeLayout>


注:如果不知道ResizeLayout是什么,百度一下吧。




下面是核心代码,代码不完整,只提供思路:

private ResizeLayout mainLinearLayout;
private View mKeyBoardPlaceView;

mainLinearLayout = (ResizeLayout) findViewById(R.id.mainLinearLayout);
mKeyBoardPlaceView = findViewById(R.id.keyboard_place_view);

private void setKeyBoardListener() {
mainLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (mIsShowExpression) { // mIsShowExpression 在表情按钮的onClick事件中切换true或false
int[] location = new int[2];
mKeyBoardPlaceView.getLocationOnScreen(location);
// mScreenHeight 是屏幕高度,在这之前获取
if (location[1] >= mScreenHeight) {
// linearLayoutExpression 是表情键盘
linearLayoutExpression.setVisibility(View.VISIBLE);
return;
}
}
}
});
}


好啦,就是这么多。如果你有更好的方式,望赐教。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: