From 3fdc062a934c84eada45ca6c9ad4671888fadb19 Mon Sep 17 00:00:00 2001 From: princekin <13063218706@163.com> Date: Sat, 17 Aug 2019 13:50:20 +0800 Subject: [PATCH] Add InputMethodUtils. --- README.md | 45 ++++++++++------ .../permission/PermissionFragment.kt | 2 - .../easyfloat/permission/rom/MeizuUtils.java | 1 - .../lzf/easyfloat/utils/InputMethodUtils.kt | 53 +++++++++++++++++++ .../widget/appfloat/AppFloatManager.kt | 6 +-- .../example/activity/ThirdActivity.kt | 32 ++++++++++- .../src/main/res/layout/activity_third.xml | 8 +-- example/src/main/res/layout/float_edit.xml | 31 +++++++++++ 8 files changed, 152 insertions(+), 26 deletions(-) create mode 100644 easyfloat/src/main/java/com/lzf/easyfloat/utils/InputMethodUtils.kt create mode 100644 example/src/main/res/layout/float_edit.xml diff --git a/README.md b/README.md index 86f37af..2c7346c 100644 --- a/README.md +++ b/README.md @@ -3,17 +3,17 @@ > [EasyFloat:浮窗从未如此简单](https://www.jianshu.com/p/7d1a7c82094a) ### 特点功能: -- 支持单页面浮窗,无需权限申请 -- 支持全局浮窗、应用前台浮窗,需要授权悬浮窗权限 -- 自动权限检测、自动跳转浮窗权限管理页、自动处理授权结果 -- 支持系统浮窗的页面过滤 -- 支持拖拽,支持各种状态的回调 -- 支持默认位置的设定,支持对齐方式和偏移量的设定 -- 支持创建多个单页面浮窗、多个系统浮窗,Tag进行区分 -- 支持出入动画的设定,有默认动画,可自行替换(策略模式) -- 使用简单、链式调用,无侵入性 -- 支持xml直接使用,满足拖拽控件的需求 -- 支持解锁更多姿势,如:拖拽缩放、通知弹窗... +- **支持单页面浮窗,无需权限申请** +- **支持全局浮窗、应用前台浮窗,需要授权悬浮窗权限** +- **自动权限检测、自动跳转浮窗权限管理页、自动处理授权结果** +- **支持系统浮窗的页面过滤** +- **支持拖拽,支持各种状态的回调** +- **支持默认位置的设定,支持对齐方式和偏移量的设定** +- **支持创建多个单页面浮窗、多个系统浮窗,Tag进行区分** +- **支持出入动画的设定,有默认动画,可自行替换(策略模式)** +- **使用简单、链式调用,无侵入性** +- **支持xml直接使用,满足拖拽控件的需求** +- **支持解锁更多姿势,如:拖拽缩放、通知弹窗...** |权限申请|系统浮窗| @@ -37,7 +37,7 @@ allprojects { - 在应用模块的`build.gradle`添加: ``` dependencies { - implementation 'com.github.princekin-f:EasyFloat:1.0.2' + implementation 'com.github.princekin-f:EasyFloat:1.0.3' } ``` @@ -133,7 +133,7 @@ setDragEnable(activity: Activity? = null, dragEnable: Boolean, floatTag: String? isShow(activity: Activity? = null, floatTag: String? = null) ``` -补充一下:**`? = null` 代表可选参数,不填也行,默认值为null。下同。** +**补充一下:`? = null` 代表可选参数,不填也行,默认值为null。下同。** #### 系统浮窗的相关API: ``` @@ -168,6 +168,16 @@ removeFilters(tag: String? = null, vararg clazz: Class<*>) clearFilters(tag: String? = null) ``` +#### 系统浮窗中使用`EditText`: +**1,为`EditText`设置点击事件,调用`openInputMethod`方法:** +``` +InputMethodUtils.openInputMethod(editText, tag) +``` +**2,软键盘关闭时,调用`closedInputMethod`方法:** +``` +InputMethodUtils.closedInputMethod(tag) +``` + #### 直接在xml布局使用拖拽控件: ``` ``` -需要为FloatingView设置点击事件,不然无法拖拽: +**需要为FloatingView设置点击事件,不然无法拖拽:** ``` floatingView.setOnClickListener {} ``` @@ -198,6 +208,11 @@ floatingView.setOnClickListener {} ### 更新日志: -#### v 1.0.2: +#### v 1.0.3: +- 修改魅族手机,权限申请回调异常的问题; +- 为系统浮窗的`EditText`,提供了软键盘的打开、关闭后的焦点移除; +- 但暂未提供软键盘的关闭监听方案,希望大家一起努力。 + +#### v 1.0.2: - 修改`enum`包名,解决Java特殊路径无法调用的问题; - 添加`@JvmOverloads`注解,支持对Java的方法重载。 diff --git a/easyfloat/src/main/java/com/lzf/easyfloat/permission/PermissionFragment.kt b/easyfloat/src/main/java/com/lzf/easyfloat/permission/PermissionFragment.kt index 06f7c12..b5a2057 100644 --- a/easyfloat/src/main/java/com/lzf/easyfloat/permission/PermissionFragment.kt +++ b/easyfloat/src/main/java/com/lzf/easyfloat/permission/PermissionFragment.kt @@ -1,6 +1,5 @@ package com.lzf.easyfloat.permission -import android.annotation.SuppressLint import android.app.Activity import android.app.Fragment import android.content.Intent @@ -20,7 +19,6 @@ internal class PermissionFragment : Fragment() { companion object { private var onPermissionResult: OnPermissionResult? = null - @SuppressLint("CommitTransaction") fun requestPermission(activity: Activity, onPermissionResult: OnPermissionResult) { this.onPermissionResult = onPermissionResult activity.fragmentManager diff --git a/easyfloat/src/main/java/com/lzf/easyfloat/permission/rom/MeizuUtils.java b/easyfloat/src/main/java/com/lzf/easyfloat/permission/rom/MeizuUtils.java index 099acba..21476fc 100755 --- a/easyfloat/src/main/java/com/lzf/easyfloat/permission/rom/MeizuUtils.java +++ b/easyfloat/src/main/java/com/lzf/easyfloat/permission/rom/MeizuUtils.java @@ -39,7 +39,6 @@ public static void applyPermission(Fragment fragment) { Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC"); // intent.setClassName("com.meizu.safe", "com.meizu.safe.security.AppSecActivity");//remove this line code for fix flyme6.3 intent.putExtra("packageName", fragment.getActivity().getPackageName()); - intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); fragment.startActivityForResult(intent, PermissionUtils.requestCode); } catch (Exception e) { try { diff --git a/easyfloat/src/main/java/com/lzf/easyfloat/utils/InputMethodUtils.kt b/easyfloat/src/main/java/com/lzf/easyfloat/utils/InputMethodUtils.kt new file mode 100644 index 0000000..2b6d973 --- /dev/null +++ b/easyfloat/src/main/java/com/lzf/easyfloat/utils/InputMethodUtils.kt @@ -0,0 +1,53 @@ +package com.lzf.easyfloat.utils + +import android.content.Context.INPUT_METHOD_SERVICE +import android.os.Handler +import android.os.Looper +import android.view.WindowManager +import android.view.inputmethod.InputMethodManager +import android.widget.EditText +import com.lzf.easyfloat.service.FloatService + +/** + * @author: liuzhenfeng + * @function: 软键盘工具类 + * 1,解决系统浮窗内的EditText,无法弹起软键盘的问题; + * 2,暂未提供软键盘关闭的监听,需要使用者自行处理,希望各位大佬共同提供解决方案。 + * @date: 2019-08-17 11:11 + */ +object InputMethodUtils { + + /** + * 让系统浮窗获取焦点,并打开软键盘 + */ + @JvmStatic + @JvmOverloads + fun openInputMethod(editText: EditText, tag: String? = null) { + FloatService.floatMap[tag ?: FloatService.DEFAULT_TAG]?.run { + // 更改flags,并刷新布局,让系统浮窗获取焦点 + params.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL + windowManager.updateViewLayout(frameLayout, params) + } + + Handler(Looper.getMainLooper()).postDelayed({ + // 打开软键盘 + val inputManager = + editText.context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager? + inputManager?.showSoftInput(editText, 0) + }, 100) + } + + /** + * 当软键盘关闭时,调用此方法,移除系统浮窗的焦点,不然系统返回键无效 + */ + @JvmStatic + @JvmOverloads + fun closedInputMethod(tag: String? = null) { + FloatService.floatMap[tag ?: FloatService.DEFAULT_TAG]?.run { + params.flags = + WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE + windowManager.updateViewLayout(frameLayout, params) + } + } + +} \ No newline at end of file diff --git a/easyfloat/src/main/java/com/lzf/easyfloat/widget/appfloat/AppFloatManager.kt b/easyfloat/src/main/java/com/lzf/easyfloat/widget/appfloat/AppFloatManager.kt index a76ceec..e7c82f0 100644 --- a/easyfloat/src/main/java/com/lzf/easyfloat/widget/appfloat/AppFloatManager.kt +++ b/easyfloat/src/main/java/com/lzf/easyfloat/widget/appfloat/AppFloatManager.kt @@ -23,9 +23,9 @@ import com.lzf.easyfloat.utils.DisplayUtils */ internal class AppFloatManager(val context: Context, var config: FloatConfig) { - private lateinit var windowManager: WindowManager - private lateinit var params: WindowManager.LayoutParams - private var frameLayout: ParentFrameLayout? = null + lateinit var windowManager: WindowManager + lateinit var params: WindowManager.LayoutParams + var frameLayout: ParentFrameLayout? = null private lateinit var touchUtils: TouchUtils diff --git a/example/src/main/java/com/lzf/easyfloat/example/activity/ThirdActivity.kt b/example/src/main/java/com/lzf/easyfloat/example/activity/ThirdActivity.kt index 80a7f2e..60b61d9 100644 --- a/example/src/main/java/com/lzf/easyfloat/example/activity/ThirdActivity.kt +++ b/example/src/main/java/com/lzf/easyfloat/example/activity/ThirdActivity.kt @@ -5,6 +5,8 @@ import android.os.Bundle import android.view.Gravity import android.view.MotionEvent import android.view.View +import android.widget.EditText +import android.widget.TextView import com.lzf.easyfloat.EasyFloat import com.lzf.easyfloat.anim.AppFloatDefaultAnimator import com.lzf.easyfloat.anim.DefaultAnimator @@ -13,10 +15,12 @@ import com.lzf.easyfloat.enums.SidePattern import com.lzf.easyfloat.example.R import com.lzf.easyfloat.interfaces.OnFloatCallbacks import com.lzf.easyfloat.interfaces.OnInvokeView +import com.lzf.easyfloat.utils.InputMethodUtils +import kotlinx.android.synthetic.main.activity_third.* /** * @author: liuzhenfeng - * @function: + * @function: 测试EditText * @date: 2019-07-26 13:13 */ class ThirdActivity : Activity() { @@ -24,6 +28,32 @@ class ThirdActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_third) + + openEditTextFloat.setOnClickListener { + showEditTextFloat() + } + } + + private fun showEditTextFloat(tag: String? = "editTextFloat") { + EasyFloat.with(this) + .setLayout(R.layout.float_edit) + .setShowPattern(ShowPattern.ALL_TIME) + .setGravity(Gravity.CENTER) + .setTag(tag) + .invokeView(OnInvokeView { + it.findViewById(R.id.editText).setOnClickListener { et -> + InputMethodUtils.openInputMethod(et as EditText, tag) + } + + it.findViewById(R.id.tvImmClosed).setOnClickListener { + InputMethodUtils.closedInputMethod(tag) + } + + it.findViewById(R.id.tvCloseFloat).setOnClickListener { + EasyFloat.dismissAppFloat(this, tag) + } + }) + .show() } private fun showFloat() { diff --git a/example/src/main/res/layout/activity_third.xml b/example/src/main/res/layout/activity_third.xml index 9a27459..4717e70 100644 --- a/example/src/main/res/layout/activity_third.xml +++ b/example/src/main/res/layout/activity_third.xml @@ -6,9 +6,9 @@ android:orientation="vertical"> + android:id="@+id/openEditTextFloat" + style="@style/TextStyle2" + android:layout_marginTop="120dp" + android:text="OpenEditTextFloat" /> \ No newline at end of file diff --git a/example/src/main/res/layout/float_edit.xml b/example/src/main/res/layout/float_edit.xml new file mode 100644 index 0000000..ea3fae8 --- /dev/null +++ b/example/src/main/res/layout/float_edit.xml @@ -0,0 +1,31 @@ + + + + + + + + + + \ No newline at end of file