DialogFragment 内含EditText, 弹出软键盘后,输入字符,显示输入的内容会出现卡顿

1,287 阅读1分钟

I/Choreographer: Skipped 109 frames! The application may be doing too much work on its main thread.

I/Choreographer: Skipped 46 frames! The application may be doing too much work on its main thread.

log 会出现类似上面的的打印,

DDMS 调试打卡,找了半天,没有发现人为函数导致的循环调用,

但是会出现布局里比如:ConstrainLayout 等布局 measure打印

原因

软键盘模式**SOFT_INPUT_ADJUST_RESIZE**为例,当二级页面弹出软键盘时,一级页面也会重新绘制,页面复杂的话容易造成卡顿现象。

当你的dialog 作为一个二级界面弹出的时候,被盖住的界面如果没有onPause, 而且如果底下的一级界面是个Activity 并且设置了windowSoftInputMode,这个mode 不为ajustNoting,会导致一级界面重绘

这个一级界面如果比较复杂,那么你就会发现有卡顿,就会从日志中发现上面的打印

解决:

去掉Activity 所在Mainfeast. 中设置的****windowSoftInputMode,因为他有个默认值,

SOFT_INPUT_ADJUST_UNSPECIFIED = 0,

1、可以直接在****Mainfeast 设置成SOFT_INPUT_ADJUST_NOTHING

2、在DialogFragment 中onResume动态设置:

private var mSoftInputMode = -1

override fun onResume() {    
 super.onResume()    
 val window = activity?.window    
    if (window != null) {        
    val attrs: WindowManager.LayoutParams? = window.attributes       
     if (attrs != null) {            
     mSoftInputMode = attrs.softInputMode           
     window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING)   
     } 
   }}
   
override fun onPause() {   
 super.onPause()    
 if (mSoftInputMode != -1) {       
     activity?.window?.setSoftInputMode(mSoftInputMode)  
  }}

3、去优化你的一级界面吧,不要嵌套太多,不要太复杂~~~~