老熟女激烈的高潮_日韩一级黄色录像_亚洲1区2区3区视频_精品少妇一区二区三区在线播放_国产欧美日产久久_午夜福利精品导航凹凸

重慶分公司,新征程啟航

為企業提供網站建設、域名注冊、服務器等服務

Android開發-獲取系統輸入法高度的正確姿勢

問題與解決

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

為攀枝花等地區用戶提供了全套網頁設計制作服務,及攀枝花網站建設行業解決方案。主營業務為做網站、成都網站設計、攀枝花網站設計,以傳統方式定制建設網站,并提供域名空間備案等一條龍服務,秉承以專業、用心的態度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!

查閱了網上很多資料,試過以后都不理想。

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

但是當Activity配置為"android:windowSoftInputMode="adjustNothing""時,布局不會在輸入法彈出時進行調整,上面的方式就會撲街。

不過經過一番探索和測試,終于發現了一種方式可以在即使設置為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中的使用進行舉例。

實現接口

引入這兩個類后,在當前Activity中實現接口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);
}
定義并初始化

在當前Activity定義成員變量,并在onCreate()中進行初始化

private KeyboardHeightProvider mKeyboardHeightProvider;

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

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

@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();
}

總結

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

如有更多疑問,請參考我的其它Android相關博客:我的博客地址


網頁標題:Android開發-獲取系統輸入法高度的正確姿勢
轉載來于:http://www.xueling.net.cn/article/jocphi.html

其他資訊

在線咨詢
服務熱線
服務熱線:028-86922220
TOP
主站蜘蛛池模板: 裸体超大乳抖乳露双乳呻吟 | 国产免费午夜 | 国产猛男GAYB0Y1069麻豆 | 亚洲色大成网站www久久九九 | 欧美激情小视频 | 97香蕉超级碰碰碰久久兔费 | 亚洲国产中文在线视频 | 亚洲一区二区三区日韩 | 国产精品乱码高清在线看 | 无码人妻AV一区二区三区波多野 | 欧美在线看 | 成人av影片在线观看 | 亚洲精品日韩综合观看成人91 | w两个世界完整免费观看超清完整 | 国产剧情无码播放在线观看 | 国产精品一级大片 | 天天操网站 | 91视频免费网址 | 热久久国产精品 | 国内91视频| 韩国V欧美V亚洲V日本V | 日本免费一区二区三区最新vr | 99在线免费视频 | 欧美国内亚洲 | 99国产欧美久久久精品 | 18出禁止看的啪视频网站 | 青楼传媒成年免费网站 | 亚洲成AV人无码综合在线观看 | 国产一二三视频 | 国产欧美日产香蕉视频 | 邻居少妇张开腿让我爽了一夜 | 中文字幕第3页 | 亚洲精品天堂成人片AV在线播放 | 蜜桃嫩草 | 亚洲短视频在线观看 | 看特级黄色片 | 久久不卡日韩美女 | 国产艹逼 | 久久不见久久见中文字幕免费 | 小早川怜子痴女在线精品视频 | 中文字幕亚洲综合久久久软件 |