偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

鴻蒙輸入框被軟鍵盤(pán)遮擋的解決辦法

運(yùn)維 系統(tǒng)運(yùn)維
滾動(dòng)操作為什么要delay 100毫秒?因?yàn)辄c(diǎn)擊一個(gè)輸入框Component.LayoutRefreshedListener有時(shí)會(huì)反復(fù)調(diào)用多次,而且間隔時(shí)間小于10毫秒,所以會(huì)造成滾動(dòng)距離不準(zhǔn)確。

[[410742]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

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

https://harmonyos.51cto.com

處理前后對(duì)比
 

問(wèn)題現(xiàn)狀

安卓上面,輸入框被軟鍵盤(pán)遮擋,很簡(jiǎn)單

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

 這樣,軟鍵盤(pán)彈出后,輸入框就會(huì)自動(dòng)上移。

鴻蒙上也有類(lèi)似的設(shè)置,但是貌似沒(méi)效果:

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

解決過(guò)程

原理:

布局文件用ScrollView包起來(lái)

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

滾動(dòng)ScrollView,使當(dāng)前焦點(diǎn)控件顯示在軟鍵盤(pán)上方。

核心代碼:

  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];//焦點(diǎn)控件的左上角 
  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)欄(手機(jī)時(shí)間、wifi顯示的那一部分,) 2211,狀態(tài)欄是129,加起來(lái)就是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.                 //包括標(biāo)題欄,但不包括狀態(tài)欄。默認(rèn) 大小 (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.                     //剛進(jìn)入界面可能為0 
  46.                     return
  47.                 } 
  48.                 int softHeight = screenHeight - decorRect.getHeight(); 
  49.                 Timber.d("onRefreshed() called with: softHeight = [ %s ]", softHeight); 
  50.  
  51.                 if (softHeight > 100) {//當(dāng)輸入法高度大于100判定為輸入法打開(kāi)了 
  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.     } 

 完整代碼見(jiàn)文末 

特別說(shuō)明: 滾動(dòng)操作為什么要delay 100毫秒?因?yàn)辄c(diǎn)擊一個(gè)輸入框Component.LayoutRefreshedListener有時(shí)會(huì)反復(fù)調(diào)用多次,而且間隔時(shí)間小于10毫秒,所以會(huì)造成滾動(dòng)距離不準(zhǔn)確。用postTask之后,每次調(diào)用的時(shí)候會(huì)把之前的task remove掉,以最新的一次為準(zhǔn)。

計(jì)算滾動(dòng)距離

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

所以,要滾動(dòng)的距離就是圖中的C=A-B。

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

可以?xún)?yōu)化的點(diǎn):

如果是Dialog中的輸入框,當(dāng)前的計(jì)算方法是否正確?

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

抽取出工具類(lèi)或工具方法,代碼復(fù)用。

文章相關(guān)附件可以點(diǎn)擊下面的原文鏈接前往下載

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

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

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

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來(lái)源: 鴻蒙社區(qū)
相關(guān)推薦

2009-08-21 13:25:49

C#打開(kāi)軟鍵盤(pán)

2019-04-25 10:20:22

H5軟鍵盤(pán)前端

2010-01-15 09:38:08

磁盤(pán)被寫(xiě)保護(hù)解決辦法

2013-04-01 17:05:28

2021-08-07 15:31:45

Windows 10Windows微軟

2013-06-27 17:26:01

AndroidEditText

2024-03-06 09:16:57

PAD設(shè)備kikaInput鴻蒙

2010-05-04 13:52:00

Oracle用戶(hù)被鎖

2024-05-06 08:28:09

Android窗口鍵盤(pán)

2017-07-03 17:20:55

Android軟鍵盤(pán)控制開(kāi)發(fā)問(wèn)題

2020-03-24 09:34:00

移動(dòng)端H5軟鍵盤(pán)

2017-12-05 15:26:19

2017-12-05 13:12:35

Android軟鍵盤(pán)參數(shù)

2020-09-24 14:06:19

Vue

2009-06-03 16:41:21

Eclipse亂碼Eclipse

2011-03-04 13:07:47

Filezilla

2021-09-27 14:44:48

鴻蒙HarmonyOS應(yīng)用

2011-01-19 17:54:48

2009-05-31 09:07:35

Oracle鎖定

2010-08-19 15:40:34

DIVIE6
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)