android近期心得整理

 

activity中OnAttachedWindow生命周期在OnResume之後,所以對長寬獲取在推薦在OnAttachedWindow中進行。onDetachedWindow是在OnDestroy之後調用的,按返回鍵會執行,但是按home鍵不會執行。對於xml中的fragment標簽隻能是android.support.v4包裡面的fragment及繼承自該類的子類,否則會報 "binary XML file line #6: Error inflating class fragment"錯誤,如果要使用3.0以上版本的fragment 建議是在xml裡面定義一個容器,然後用FragmentTransaction操作容器,添加fragment。TextView的singeLine會使自定義背景的顏色失效。對於EditText控制可編輯性最好的解決辦法就是設置enable的值來控制,比通過代碼調整或者替換控件的方法要好的多。監聽鍵盤的彈起和隱藏事件,推薦是在AndroidManifest.xml對activity的WindowSoftInputMode中設置adjustPan或者adjustResize,具體看哪種適合你的使用環境,然後註冊監聽ViewTreeObserver.OnGlobalLayoutListener,在重載方法onGlobalLayout中判斷彈起還是隱藏。示例代碼如下:

private class MyStateListener implements ViewTreeObserver.OnGlobalLayoutListener {
        private final View mDecorView;
        private final CameraFragment mFragment;
        private int mFrameHeight = 0;
        private boolean mKeyboardShow;
        private final Rect mVisibleDisplayFrame;

        IMEStateListener(CameraFragment paramView, View view) {
            this.mFragment = paramView;
            this.mDecorView = view;
            this.mVisibleDisplayFrame = new Rect();
        }

        @Override
        public void onGlobalLayout() {
            this.mDecorView.getWindowVisibleDisplayFrame(mVisibleDisplayFrame);
            int i = this.mVisibleDisplayFrame.height();
            if (this.mFrameHeight == 0) {
                this.mFrameHeight = i;
            }
            if (i < this.mFrameHeight) {
                this.mKeyboardShow = true;
            } else if (this.mKeyboardShow) {
                this.mKeyboardShow = false;
                mHandler.sendEmptyMessage(KEYBOARD_HIDE);
            }
        }
    }

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *