欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)教程之獲取系統(tǒng)輸入法高度的正確姿勢

 更新時間:2018年10月21日 15:32:56   作者:ExampleCode  
這篇文章主要給大家介紹了關(guān)于Android開發(fā)教程之獲取系統(tǒng)輸入法高度的正確姿勢,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Android具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

問題與解決

在Android應(yīng)用的開發(fā)中,有一些需求需要我們獲取到輸入法的高度,但是官方的API并沒有提供類似的方法,所以我們需要自己來實現(xiàn)。

查閱了網(wǎng)上很多資料,試過以后都不理想。

比如有的方法通過監(jiān)聽布局的變化來計算輸入法的高度,這種方式在Activity的配置中配置為"android:windowSoftInputMode="adjustResize""時沒有問題,可以正確獲取輸入法的高度,因為布局此時確實會動態(tài)的調(diào)整。

但是當(dāng)Activity配置為"android:windowSoftInputMode="adjustNothing""時,布局不會在輸入法彈出時進(jìn)行調(diào)整,上面的方式就會撲街。

不過經(jīng)過一番探索和測試,終于發(fā)現(xiàn)了一種方式可以在即使設(shè)置為adjustNothing時也可以正確計算高度放方法。

同時也感謝這位外國朋友:

GitHub地址

方法如下

其實也就兩個類,我也做了一些修改,解決了一些問題,這里也貼出來:

KeyboardHeightObserver.java

/**
 * The observer that will be notified when the height of 
 * the keyboard has changed
 */
public interface KeyboardHeightObserver {

 /** 
  * Called when the keyboard height has changed, 0 means keyboard is closed,
  * >= 1 means keyboard is opened.
  * 
  * @param height  The height of the keyboard in pixels
  * @param orientation The orientation either: Configuration.ORIENTATION_PORTRAIT or 
  *      Configuration.ORIENTATION_LANDSCAPE
  */
 void onKeyboardHeightChanged(int height, int orientation);
}

KeyboardHeightProvider.java

/**
 * The keyboard height provider, this class uses a PopupWindow
 * to calculate the window height when the floating keyboard is opened and closed. 
 */
public class KeyboardHeightProvider extends PopupWindow {

 /** The tag for logging purposes */
 private final static String TAG = "sample_KeyboardHeightProvider";

 /** The keyboard height observer */
 private KeyboardHeightObserver observer;

 /** The cached landscape height of the keyboard */
 private int keyboardLandscapeHeight;

 /** The cached portrait height of the keyboard */
 private int keyboardPortraitHeight;

 /** The view that is used to calculate the keyboard height */
 private View popupView;

 /** The parent view */
 private View parentView;

 /** The root activity that uses this KeyboardHeightProvider */
 private Activity activity;

 /** 
  * Construct a new KeyboardHeightProvider
  * 
  * @param activity The parent activity
  */
 public KeyboardHeightProvider(Activity activity) {
  super(activity);
  this.activity = activity;

  LayoutInflater inflator = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
  this.popupView = inflator.inflate(R.layout.keyboard_popup_window, null, false);
  setContentView(popupView);

  setSoftInputMode(LayoutParams.SOFT_INPUT_ADJUST_RESIZE | LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
  setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);

  parentView = activity.findViewById(android.R.id.content);

  setWidth(0);
  setHeight(LayoutParams.MATCH_PARENT);

  popupView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
     if (popupView != null) {
      handleOnGlobalLayout();
     }
    }
   });
 }

 /**
  * Start the KeyboardHeightProvider, this must be called after the onResume of the Activity.
  * PopupWindows are not allowed to be registered before the onResume has finished
  * of the Activity.
  */
 public void start() {

  if (!isShowing() && parentView.getWindowToken() != null) {
   setBackgroundDrawable(new ColorDrawable(0));
   showAtLocation(parentView, Gravity.NO_GRAVITY, 0, 0);
  }
 }

 /**
  * Close the keyboard height provider, 
  * this provider will not be used anymore.
  */
 public void close() {
  this.observer = null;
  dismiss();
 }

 /** 
  * Set the keyboard height observer to this provider. The 
  * observer will be notified when the keyboard height has changed. 
  * For example when the keyboard is opened or closed.
  * 
  * @param observer The observer to be added to this provider.
  */
 public void setKeyboardHeightObserver(KeyboardHeightObserver observer) {
  this.observer = observer;
 }
 
 /**
  * Get the screen orientation
  *
  * @return the screen orientation
  */
 private int getScreenOrientation() {
  return activity.getResources().getConfiguration().orientation;
 }

 /**
  * Popup window itself is as big as the window of the Activity. 
  * The keyboard can then be calculated by extracting the popup view bottom 
  * from the activity window height. 
  */
 private void handleOnGlobalLayout() {

  Point screenSize = new Point();
  activity.getWindowManager().getDefaultDisplay().getSize(screenSize);

  Rect rect = new Rect();
  popupView.getWindowVisibleDisplayFrame(rect);

  // REMIND, you may like to change this using the fullscreen size of the phone
  // and also using the status bar and navigation bar heights of the phone to calculate
  // the keyboard height. But this worked fine on a Nexus.
  int orientation = getScreenOrientation();
  int keyboardHeight = screenSize.y - rect.bottom;
  
  if (keyboardHeight == 0) {
   notifyKeyboardHeightChanged(0, orientation);
  }
  else if (orientation == Configuration.ORIENTATION_PORTRAIT) {
   this.keyboardPortraitHeight = keyboardHeight; 
   notifyKeyboardHeightChanged(keyboardPortraitHeight, orientation);
  } 
  else {
   this.keyboardLandscapeHeight = keyboardHeight; 
   notifyKeyboardHeightChanged(keyboardLandscapeHeight, orientation);
  }
 }

 private void notifyKeyboardHeightChanged(int height, int orientation) {
  if (observer != null) {
   observer.onKeyboardHeightChanged(height, orientation);
  }
 }
}

使用方法

此處以在Activity中的使用進(jìn)行舉例。

實現(xiàn)接口

引入這兩個類后,在當(dāng)前Activity中實現(xiàn)接口KeyboardHeightObserver:

@Override
public void onKeyboardHeightChanged(int height, int orientation) {
 String or = orientation == Configuration.ORIENTATION_PORTRAIT ? "portrait" : "landscape";
 Logger.d(TAG, "onKeyboardHeightChanged in pixels: " + height + " " + or);
}

定義并初始化

在當(dāng)前Activity定義成員變量,并在onCreate()中進(jìn)行初始化

private KeyboardHeightProvider mKeyboardHeightProvider;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
 ...
 mKeyboardHeightProvider = new KeyboardHeightProvider(this);
 new Handler().post(() -> mKeyboardHeightProvider.start());
}

生命周期處理

初始化完成后,我們要在Activity中的生命周期中也要進(jìn)行處理,以免內(nèi)存泄露。

@Override
protected void onResume() {
 super.onResume();
 mKeyboardHeightProvider.setKeyboardHeightObserver(this);
}

@Override
protected void onPause() {
 super.onPause();
 mKeyboardHeightProvider.setKeyboardHeightObserver(null);
}

@Override
protected void onDestroy() {
 super.onDestroy();
 mKeyboardHeightProvider.close();
}

總結(jié)

此時我們就可以正確獲取的當(dāng)前輸入法的高度了,即使android:windowSoftInputMode="adjustNothing"時也可以正確獲取到,這正是這個方法的強(qiáng)大之處,利用這個方法可以實現(xiàn)比如類似微信聊天的界面,流暢切換輸入框,表情框等。

好了,以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關(guān)文章

  • Android自定義Toolbar使用方法詳解

    Android自定義Toolbar使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android自定義Toolbar使用方法 ,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-12-12
  • Android開發(fā)者常見的UI組件總結(jié)大全

    Android開發(fā)者常見的UI組件總結(jié)大全

    Android開發(fā)中UI組件是構(gòu)建用戶界面的基本元素,下面這篇文章主要給大家介紹了關(guān)于Android開發(fā)者常見的UI組件總結(jié)的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-04-04
  • 安卓(Android)實現(xiàn)選擇時間功能

    安卓(Android)實現(xiàn)選擇時間功能

    安卓開發(fā)過程中難免會碰到需要選擇日期時間的情況,當(dāng)然不可能讓用戶自己輸入日期時間,小編收集整理了一些資料,總結(jié)了一下如何實現(xiàn)android選擇時間的功能,方便后來者參考
    2016-08-08
  • Android App多個入口的實現(xiàn)方法

    Android App多個入口的實現(xiàn)方法

    這篇文章主要介紹了Android App多個入口的實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • Android上傳文件到Web服務(wù)器 PHP接收文件

    Android上傳文件到Web服務(wù)器 PHP接收文件

    這篇文章主要為大家詳細(xì)介紹了Android上傳文件到Web服務(wù)器,PHP接收文件的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的設(shè)置小結(jié)

    A07_TimePicker & DatePicker & AnalogClock & Digi

    本文將帶領(lǐng)大家一起學(xué)習(xí)時間日期和時鐘的設(shè)置。A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的設(shè)置,感興趣的朋友可以參考下哈
    2013-06-06
  • Android自定義控件實現(xiàn)帶文本與數(shù)字的圓形進(jìn)度條

    Android自定義控件實現(xiàn)帶文本與數(shù)字的圓形進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了Android自定義控件實現(xiàn)帶文本與數(shù)字的圓形進(jìn)度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Android進(jìn)程通信之Messenger和AIDL使用詳解

    Android進(jìn)程通信之Messenger和AIDL使用詳解

    本篇文章主要介紹了Android進(jìn)程通信之Messenger和AIDL使用詳解,具有一定的參考價值,有興趣的可以了解一下。
    2017-01-01
  • Android如何實現(xiàn)APP自動更新

    Android如何實現(xiàn)APP自動更新

    現(xiàn)在一般的android軟件都是需要不斷更新的,當(dāng)你打開某個app的時候,如果有新的版本,它會提示你有新版本需要更新。該小程序?qū)崿F(xiàn)的就是這個功能。有需要的朋友們可以參考借鑒。
    2016-08-08
  • Android開發(fā)實現(xiàn)的電話竊聽和攔截應(yīng)用

    Android開發(fā)實現(xiàn)的電話竊聽和攔截應(yīng)用

    這篇文章主要介紹了Android開發(fā)實現(xiàn)的電話竊聽和攔截應(yīng)用,結(jié)合實例形式分析了Android針對電話的監(jiān)聽與攔截的相關(guān)技巧,需要的朋友可以參考下
    2016-08-08

最新評論