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是什么,百度一下吧。
下面是核心代码,代码不完整,只提供思路:
好啦,就是这么多。如果你有更好的方式,望赐教。
我的解决方案是:在输入法键盘完全隐藏后,再将表情键盘显示。
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; } } } }); }
好啦,就是这么多。如果你有更好的方式,望赐教。
相关文章推荐
- Android开发总结笔记 SearchView&Search(搜索相关) 1-3-7
- 【Android】图片模糊效果,Blur image with glide or picasso
- Android源码开发之横屏actionbar的title消失
- Android中使用代码截图的各种方法总结
- 在git hub上面写android / ios的 sample code 集合。
- android的R.java无法修改解决!
- 在git hub上面写android / ios的 sample code 集合。
- android spinner 设置弹框和下拉方式
- android学习笔记14 - android.os包:系统功能调用和系统参数获取
- 使用Android Studio调试内存问题
- android保存图片到图库
- android studio下Material Design库的使用
- Android4.0的Alertdialog对话框,设置点击其他位置不消失
- Android性能优化之避免内存泄漏的建议
- Android 通过监听收信箱获取短信,Cursor对象字段的详细信息
- android 多分辨率机型适配文件夹命名规则
- [Android]startActivityForResult启动singleTask的Activity,则onActivitResult()立即回调且resultCode为RESULT_CANCEL
- Android第三方资源使用之ImageCache
- Android第三方资源使用之ImageCache
- Android 四大组件之Service