最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

問(wèn)題與解決

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

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

比如有的方法通過(guò)監(jiān)聽(tīng)布局的變化來(lái)計(jì)算輸入法的高度,這種方式在Activity的配置中配置為"android:windowSoftInputMode="adjustResize""時(shí)沒(méi)有問(wèn)題,可以正確獲取輸入法的高度,因?yàn)椴季执藭r(shí)確實(shí)會(huì)動(dòng)態(tài)的調(diào)整。

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

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

同時(shí)也感謝這位外國(guó)朋友:

GitHub地址

方法如下

其實(shí)也就兩個(gè)類,我也做了一些修改,解決了一些問(wèn)題,這里也貼出來(lái):

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)行舉例。

實(shí)現(xiàn)接口

引入這兩個(gè)類后,在當(dāng)前Activity中實(shí)現(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é)

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

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

相關(guān)文章

  • Android構(gòu)建Material Design應(yīng)用詳解

    Android構(gòu)建Material Design應(yīng)用詳解

    這篇文章主要為大家詳細(xì)介紹了Android構(gòu)建Material Design應(yīng)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • ListView通用泛型適配器

    ListView通用泛型適配器

    今天小編就為大家分享一篇關(guān)于ListView通用泛型適配器,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2019-01-01
  • Android 控制車載藍(lán)牙播放音樂(lè)詳解流程

    Android 控制車載藍(lán)牙播放音樂(lè)詳解流程

    本篇文章介紹了手機(jī)端音樂(lè)暫停和播放狀態(tài),從服務(wù)端告訴客戶端、設(shè)備端實(shí)現(xiàn)暫停、播放、上一首、下一首等功能的實(shí)現(xiàn),通讀本篇對(duì)大家的學(xué)習(xí)或工作具有一定的價(jià)值,需要的朋友可以參考下
    2021-10-10
  • Android自定義view實(shí)現(xiàn)倒計(jì)時(shí)控件

    Android自定義view實(shí)現(xiàn)倒計(jì)時(shí)控件

    這篇文章主要為大家詳細(xì)介紹了Android自定義view實(shí)現(xiàn)倒計(jì)時(shí)控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Android判斷11位手機(jī)號(hào)碼的方法(正則表達(dá)式)

    Android判斷11位手機(jī)號(hào)碼的方法(正則表達(dá)式)

    項(xiàng)目里頭需要做一個(gè)判斷用戶輸入的號(hào)碼是否是正確的手機(jī)號(hào)碼,正確的手機(jī)號(hào)碼應(yīng)該是11位的,這里我們需要用一個(gè)正則表達(dá)式來(lái)進(jìn)行判斷,下面我把寫(xiě)法分享給大家
    2016-12-12
  • Android 遍歷文件夾中所有文件的實(shí)例代碼

    Android 遍歷文件夾中所有文件的實(shí)例代碼

    本篇文章主要介紹了Android 遍歷文件夾中所有文件的實(shí)例代碼,可以獲得文件夾中所有文件的路徑及文件名,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-06-06
  • 基于Android AIDL進(jìn)程間通信接口使用介紹

    基于Android AIDL進(jìn)程間通信接口使用介紹

    本篇文章小編為大家介紹,基于Android AIDL進(jìn)程間通信接口使用介紹。需要的朋友參考下
    2013-04-04
  • Android 滾動(dòng)時(shí)間選擇的示例代碼

    Android 滾動(dòng)時(shí)間選擇的示例代碼

    這篇文章主要介紹了Android 滾動(dòng)時(shí)間選擇的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • Android工具欄頂出轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的實(shí)現(xiàn)方法實(shí)例

    Android工具欄頂出轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的實(shí)現(xiàn)方法實(shí)例

    這篇文章主要給大家介紹了關(guān)于Android工具欄頂出轉(zhuǎn)場(chǎng)動(dòng)畫(huà)的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Android開(kāi)發(fā)使用自定義view實(shí)現(xiàn)ListView下拉的視差特效功能

    Android開(kāi)發(fā)使用自定義view實(shí)現(xiàn)ListView下拉的視差特效功能

    這篇文章主要介紹了Android開(kāi)發(fā)使用自定義view實(shí)現(xiàn)ListView下拉的視差特效功能,結(jié)合實(shí)例形式詳細(xì)分析了Android重寫(xiě)ListView控件實(shí)現(xiàn)ListView下拉視差效果的步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-10-10

最新評(píng)論

巨鹿县| 泗阳县| 台南县| 年辖:市辖区| 淮阳县| 台东市| 永和县| 咸宁市| 绥化市| 通江县| 花垣县| 疏附县| 喜德县| 永昌县| 梓潼县| 大邑县| 弥勒县| 获嘉县| 大丰市| 台州市| 乐安县| 齐齐哈尔市| 城固县| 大方县| 喜德县| 库车县| 老河口市| 宜春市| 陇西县| 石门县| 甘谷县| 赤峰市| 余姚市| 新宾| 名山县| 白玉县| 临江市| 佛冈县| 方正县| 三明市| 长寿区|