Android監(jiān)聽軟鍵盤彈出與隱藏的兩種方法

需求:

創(chuàng)新互聯(lián)公司主要從事成都做網(wǎng)站、網(wǎng)站制作、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)衢州,十余年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220

現(xiàn)在有一個(gè)需求是點(diǎn)擊一行文本框,彈出一個(gè)之前隱藏的輸入框,輸入完成后按返回鍵或者其他的東西隱藏鍵盤和輸入框,將輸入框的內(nèi)容填充到文本框中。

實(shí)現(xiàn):

拿到這個(gè)需求的第一反應(yīng)就是寫一個(gè)監(jiān)聽來監(jiān)聽鍵盤的顯示和隱藏來控制輸入框的顯示和隱藏,控制文本框中的內(nèi)容。
所以我做了如下操作:

  1. 指定android:windowSoftInputMode="adjustResize|stateAlwaysHidden"這個(gè)的做法是為了讓鍵盤彈出時(shí)改變布局。
  2. 讓Activity實(shí)現(xiàn)LayoutchangeListener,監(jiān)聽布局的改變,當(dāng)布局發(fā)生的改變?yōu)槠聊坏?/3時(shí)我們認(rèn)為是鍵盤導(dǎo)致的。
@Override 
 public void onLayoutChange(View v, int left, int top, int right, 
     int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { 

   //old是改變前的左上右下坐標(biāo)點(diǎn)值,沒有old的是改變后的左上右下坐標(biāo)點(diǎn)值 

   //現(xiàn)在認(rèn)為只要控件將Activity向上推的高度超過了1/3屏幕高,就認(rèn)為軟鍵盤彈起 
   if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight)){ 

     Toast.makeText(MainActivity.this, "監(jiān)聽到軟鍵盤彈起...", Toast.LENGTH_SHORT).show(); 

   }else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){ 

     Toast.makeText(MainActivity.this, "監(jiān)聽到軟件盤關(guān)閉...", Toast.LENGTH_SHORT).show(); 

   } 

 }

問題:

沒錯(cuò),這樣確實(shí)是能夠做到監(jiān)聽軟鍵盤的彈出和隱藏,這一切都是因?yàn)橹霸O(shè)置了indowSoftInputMode=adjustResize,但是當(dāng)全屏模式下是這個(gè)屬性是無效的,鍵盤彈出和隱藏并不會(huì)觸發(fā)onLayouChangeListener。

而項(xiàng)目中使用了SystemBarTintManager之后,Activity就變成了全屏模式所以我做了如下操作

//contentlayout是最外層布局
mChildOfContent = contentlayout.getChildAt(0);
mChildOfContent.getViewTreeObserver()
.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {  
   public void onGlobalLayout() {    
       possiblyResizeChildOfContent();  
}});

private void possiblyResizeChildOfContent() {  
int usableHeightNow = computeUsableHeight();  
if (usableHeightNow != usableHeightPrevious) {    
   int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();    
   int heightDifference = usableHeightSansKeyboard - usableHeightNow;    
   if (heightDifference > (usableHeightSansKeyboard / 4)) {      
   // 鍵盤彈出    
   } else {      
   // 鍵盤收起      
   productInfo.setVisibility(View.GONE);      
   productInfoEnd.setText(productInfo.getText().toString());    
}    
   mChildOfContent.requestLayout();    
    usableHeightPrevious = usableHeightNow;  
}
}
private int computeUsableHeight() {  
 Rect r = new Rect();  
mChildOfContent.getWindowVisibleDisplayFrame(r);  
return (r.bottom - r.top);}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)頁標(biāo)題:Android監(jiān)聽軟鍵盤彈出與隱藏的兩種方法
網(wǎng)站網(wǎng)址:http://bm7419.com/article20/gocdjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、ChatGPT、品牌網(wǎng)站制作、網(wǎng)站維護(hù)面包屑導(dǎo)航、

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)