欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

Android開發(fā)之Dialog中隱藏鍵盤的正確使用方法

Android 開發(fā)之Dialog中隱藏鍵盤的正確使用方法

10年積累的網(wǎng)站建設(shè)、成都做網(wǎng)站經(jīng)驗,可以快速應(yīng)對客戶對網(wǎng)站的新想法和需求。提供各種問題對應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識你,你也不認(rèn)識我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有臨猗免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

場景:彈出一個Dialog,里面有一個EditText,用來輸入內(nèi)容,因為輸入時,需要彈出鍵盤,所以當(dāng)Dialog消失時,鍵盤要一起隱藏。

現(xiàn)在我們做一個自定義的Dialog

MyDialog extends Dialog 

一開始認(rèn)為這個功能很容易實現(xiàn),于是寫了下面的代碼

//Dialog的構(gòu)造函數(shù)中寫 
  this.setOnDismissListener(new OnDismissListener() { 
   @Override 
   public void onDismiss(DialogInterface dialog) { 
    hideKeyBoard(); 
   } 
  }); 
//edContent是輸入框 
 public void hideKeyBoard(){ 
  InputMethodManager inputMethodManager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
  inputMethodManager.hideSoftInputFromWindow(edContent.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 
 } 

運行之后,發(fā)現(xiàn)根本無法隱藏,看看hideSoftInputFromWindow中干了啥

public boolean hideSoftInputFromWindow(IBinder windowToken, int flags, 
    ResultReceiver resultReceiver) { 
  checkFocus(); 
  synchronized (mH) { 
    if (mServedView == null || mServedView.getWindowToken() != windowToken) { 
      return false; 
    } 
 
    try { 
      return mService.hideSoftInput(mClient, flags, resultReceiver); 
    } catch (RemoteException e) { 
    } 
    return false; 
  } 
} 

跟蹤進去發(fā)現(xiàn)參數(shù) windowToken 是 null,而且 mServedView 也是null,所以直接返回false,無法隱藏。

也就是說,你監(jiān)聽Cancel或者Dismiss都是不行的,因為此時Dialog已經(jīng)消失,用于輸入的服務(wù)窗體已經(jīng)是null了,所以你要想 隱藏鍵盤,就需要在Dismiss之前處理,那這個入口在哪呢?

為了當(dāng)點擊空白處時,可以隱藏Dialog,所以我們在構(gòu)造函數(shù)中加了一句話

this.setCanceledOnTouchOutside(true); 

所以當(dāng)我們點擊空白區(qū)域時,會觸發(fā)Dialog的onTouchEvent

public boolean onTouchEvent(MotionEvent event) { 
  if (mCancelable && mShowing && mWindow.shouldCloseOnTouch(mContext, event)) { 
    cancel(); 
    return true; 
  } 
   
  return false; 
} 

這里會調(diào)用基類Window的shouldCloseOnTouch方法,來判斷是否可以關(guān)閉,這里我們看到如果滿足,就直接cancel()了,

public void cancel() { 
  if (!mCanceled && mCancelMessage != null) { 
    mCanceled = true; 
    // Obtain a new message so this dialog can be re-used 
    Message.obtain(mCancelMessage).sendToTarget(); 
  } 
  dismiss(); 
} 

這里面就會dismiss掉Dialog,所以我們發(fā)現(xiàn),在dismiss前,我們根本無法干預(yù),真是個悲劇。所以我們只能重載onTouchEvent方法,并且自己判斷是否可以關(guān)閉(也就是把下面代碼遷移到你的代碼中!

public boolean shouldCloseOnTouch(Context context, MotionEvent event) { 
  if (mCloseOnTouchOutside && event.getAction() == MotionEvent.ACTION_DOWN 
      && isOutOfBounds(context, event) && peekDecorView() != null) { 
    return true; 
  } 
  return false; 
} 
 
private boolean isOutOfBounds(Context context, MotionEvent event) { 
  final int x = (int) event.getX(); 
  final int y = (int) event.getY(); 
  final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop(); 
  final View decorView = getDecorView(); 
  return (x < -slop) || (y < -slop) 
      || (x > (decorView.getWidth()+slop)) 
      || (y > (decorView.getHeight()+slop)); 
} 

自己代碼中這樣

@Override 
public boolean onTouchEvent(MotionEvent event) { 
 if (isShowing() && shouldCloseOnTouch(getContext(),event)){ 
  hideKeyBoard(); 
 } 
 return super.onTouchEvent(event); 
} 
public boolean shouldCloseOnTouch(Context context, MotionEvent event) { 
 if (event.getAction() == MotionEvent.ACTION_DOWN 
     && isOutOfBounds(context, event) && getWindow().peekDecorView() != null) { 
  return true; 
 } 
 return false; 
} 
private boolean isOutOfBounds(Context context, MotionEvent event) { 
 final int x = (int) event.getX(); 
 final int y = (int) event.getY(); 
 final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop(); 
 final View decorView = getWindow().getDecorView(); 
 return (x < -slop) || (y < -slop) 
     || (x > (decorView.getWidth()+slop)) 
     || (y > (decorView.getHeight()+slop)); 
} 

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

當(dāng)前文章:Android開發(fā)之Dialog中隱藏鍵盤的正確使用方法
轉(zhuǎn)載源于:http://www.chinadenli.net/article6/igpiig.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊自適應(yīng)網(wǎng)站響應(yīng)式網(wǎng)站品牌網(wǎng)站設(shè)計建站公司外貿(mào)建站

廣告

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

網(wǎng)站托管運營