自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

鴻蒙輸入框被軟鍵盤遮擋的解決辦法

運維 系統(tǒng)運維
滾動操作為什么要delay 100毫秒?因為點擊一個輸入框Component.LayoutRefreshedListener有時會反復調用多次,而且間隔時間小于10毫秒,所以會造成滾動距離不準確。

[[410742]]

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區(qū)

https://harmonyos.51cto.com

處理前后對比
 

問題現狀

安卓上面,輸入框被軟鍵盤遮擋,很簡單

  1. xml 配置 
  2. android:windowSoftInputMode="adjustPan" 
  3. 或者,java 配置 
  4. getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN); 

 這樣,軟鍵盤彈出后,輸入框就會自動上移。

鴻蒙上也有類似的設置,但是貌似沒效果:

  1. getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN); 

解決過程

原理:

布局文件用ScrollView包起來

監(jiān)聽根布局大小變化,變小了,證明輸入法彈出了。

滾動ScrollView,使當前焦點控件顯示在軟鍵盤上方。

核心代碼:

  1. public class MainAbilitySlice extends AbilitySlice { 
  2.     private EventHandler mainHandler = new EventHandler(EventRunner.getMainEventRunner()); 
  3.     private MyTask myTask = null
  4.     class MyTask implements Runnable { 
  5.         private final int softHeight; 
  6.         private final ScrollView root; 
  7.         private final Rect decorRect; 
  8.  
  9.         public MyTask(int softHeight, ScrollView root, Rect decorRect) { 
  10.             this.softHeight = softHeight; 
  11.             this.root = root; 
  12.             this.decorRect = decorRect; 
  13.         } 
  14.  
  15.         @Override 
  16.         public void run() { 
  17.             Timber.d("onRefreshed() called with: softHeight = [ %s ]", softHeight); 
  18.             Component focusView = root.findFocus(); 
  19.             int focusTop = focusView.getLocationOnScreen()[1];//焦點控件的左上角 
  20.             root.fluentScrollByY(focusTop + focusView.getHeight() - decorRect.top - decorRect.getHeight() + 100); 
  21.         } 
  22.     } 
  23.  
  24.     @Override 
  25.     public void onStart(Intent intent) { 
  26.         super.onStart(intent); 
  27.         getWindow().setInputPanelDisplayType(WindowManager.LayoutConfig.INPUT_ADJUST_PAN); 
  28.         super.setUIContent(ResourceTable.Layout_ability_main); 
  29.  
  30.         Optional<Display> display = DisplayManager.getInstance().getDefaultDisplay(getContext()); 
  31.         Point pt = new Point(); 
  32.         display.get().getSize(pt); 
  33.         int screenHeight = pt.getPointYToInt();//不包括狀態(tài)欄(手機時間、wifi顯示的那一部分,) 2211,狀態(tài)欄是129,加起來就是2340 
  34.         Timber.d("onRefreshed() called with: screenHeight = [ %s ]", screenHeight); 
  35.  
  36.         ScrollView root = (ScrollView) findComponentById(ResourceTable.Id_root); 
  37.         root.setLayoutRefreshedListener(new Component.LayoutRefreshedListener() { 
  38.             @Override 
  39.             public void onRefreshed(Component component) { 
  40.                 //包括標題欄,但不包括狀態(tài)欄。默認 大小 (0,129,1080,2340),top=129即狀態(tài)欄 , height=2211。 同android的decorView 
  41.                 Rect decorRect = new Rect(); 
  42.                 component.getWindowVisibleRect(decorRect); 
  43.                 Timber.d("onRefreshed() called with: rect = [ %s ]", decorRect); 
  44.                 if (decorRect.getHeight() == 0) { 
  45.                     //剛進入界面可能為0 
  46.                     return
  47.                 } 
  48.                 int softHeight = screenHeight - decorRect.getHeight(); 
  49.                 Timber.d("onRefreshed() called with: softHeight = [ %s ]", softHeight); 
  50.  
  51.                 if (softHeight > 100) {//當輸入法高度大于100判定為輸入法打開了 
  52.                     if (myTask != null) { 
  53.                         mainHandler.removeTask(myTask); 
  54.                         myTask = null
  55.                     } 
  56.                     mainHandler.postTask(myTask = new MyTask(softHeight, root, decorRect), 100); 
  57.                 } 
  58.             } 
  59.         }); 
  60.     } 

 完整代碼見文末 

特別說明: 滾動操作為什么要delay 100毫秒?因為點擊一個輸入框Component.LayoutRefreshedListener有時會反復調用多次,而且間隔時間小于10毫秒,所以會造成滾動距離不準確。用postTask之后,每次調用的時候會把之前的task remove掉,以最新的一次為準。

計算滾動距離

其中上面的大紅框是decorRect(即當前Ability可視區(qū)域),下面的大黑框是輸入法顯示區(qū)域。其中,軟鍵盤彈出后,輸入框被軟鍵盤擋住,圖中的小紅框。

所以,要滾動的距離就是圖中的C=A-B。

輸入框被軟鍵盤遮擋的解決辦法-鴻蒙HarmonyOS技術社區(qū)

可以優(yōu)化的點:

如果是Dialog中的輸入框,當前的計算方法是否正確?

如果不用ScrollView,還有別的解決辦法嗎?

抽取出工具類或工具方法,代碼復用。

文章相關附件可以點擊下面的原文鏈接前往下載

原文鏈接:https://harmonyos.51cto.com/posts/4776

想了解更多內容,請訪問:

51CTO和華為官方合作共建的鴻蒙技術社區(qū)

https://harmonyos.51cto.com

 

責任編輯:jianghua 來源: 鴻蒙社區(qū)
相關推薦

2009-08-21 13:25:49

C#打開軟鍵盤

2010-01-15 09:38:08

磁盤被寫保護解決辦法

2024-03-06 09:16:57

PAD設備kikaInput鴻蒙

2019-04-25 10:20:22

H5軟鍵盤前端

2013-06-27 17:26:01

AndroidEditText

2021-08-07 15:31:45

Windows 10Windows微軟

2013-04-01 17:05:28

2010-05-04 13:52:00

Oracle用戶被鎖

2024-05-06 08:28:09

Android窗口鍵盤

2017-07-03 17:20:55

Android軟鍵盤控制開發(fā)問題

2020-09-24 14:06:19

Vue

2020-03-24 09:34:00

移動端H5軟鍵盤

2017-12-05 13:12:35

Android軟鍵盤參數

2017-12-05 15:26:19

2009-06-03 16:41:21

Eclipse亂碼Eclipse

2011-03-04 13:07:47

Filezilla

2011-01-19 17:54:48

2009-05-31 09:07:35

Oracle鎖定

2021-12-13 16:44:49

鴻蒙HarmonyOS應用

2011-06-17 11:10:51

Qt 中文 輸出
點贊
收藏

51CTO技術棧公眾號