Android開發 EditView的開發記錄

自動選中焦點,而且彈出輸入框

EditText.setFocusable(true);
EditText.setFocusableInTouchMode(true);
EditText.requestFocus();
InputMethodManager inputManager = (InputMethodManager) viewHolder.commentEditText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(EditText, 0);

輸入法位置改變,改變輸入框位置

android:windowSoftInputMode的值adjustPan或者adjustResize便可,像這樣:android

<activity
    android:name=".MainActivity"
    android:windowSoftInputMode="adjustPan"  >
    ...
</activity>

這個方法在一些ListView裏使用的時候,輸入框依然會被遮蓋.app

懸浮輸入框,始終懸浮在輸入法的上方

首先是清單xml裏添加activity的屬性以下

<activity android:name=".work.HomeActivity"
            android:windowSoftInputMode="adjustPan"/>

而後是在佈局文件裏的輸入框,由於是點擊按鈕後顯示輸入框,因此下面的輸入框在佈局最下面爲隱藏狀態,ide

<EditText
        android:id="@+id/comment_edittext"
        android:layout_width="match_parent"
        android:layout_height="52dp"
        android:textSize="@dimen/font_size_14"
        android:hint="評論"
        android:textColorHint="@color/fontColorAuxiliaryLightGray"
        android:singleLine="true"
        android:visibility="gone"
        android:focusable="true"
        android:focusableInTouchMode="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

監聽輸入法是否彈出

private void initGlobalLayoutListener(){
        mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
                    int mScreenHeight = 0;
                    int mKeyboardHeight = 0;
                    @Override
                    public void onGlobalLayout() {

                        Rect rect = new Rect();
                        // 測量當前窗口的顯示區域
                        ((Activity)getContext()).getWindow().getDecorView()
                                .getWindowVisibleDisplayFrame(rect);
                        if(mScreenHeight <= 0){
                            mScreenHeight = ((WindowManager) getContext()
                                    .getSystemService(Context.WINDOW_SERVICE))
                                    .getDefaultDisplay().getHeight();
                        }
                        //計算出軟鍵盤的高度
                        int keyboardHeight = mScreenHeight - rect.bottom;

                        //若是keyboardHeight大於屏幕的五分之一,
                        // 此時keyboardHeight有效,反之就是軟鍵盤已經關閉了。
                        if (Math.abs(keyboardHeight) > mScreenHeight / 5) {
                            mKeyboardHeight = keyboardHeight;
                            if (mEdittext.getVisibility() == View.GONE){//此處多添加一次顯示,由於OnGlobalLayoutListener的連續性會致使以前未觸發鍵盤的判斷還執行,而後又隱藏了輸入框
                                mEdittext.setVisibility(View.VISIBLE);
                                mEdittext.setFocusable(true);
                                mEdittext.setFocusableInTouchMode(true);
                                mEdittext.requestFocus();
                                mEdittext.setTag(true);
                            }
                            L.e("已經觸發鍵盤");
                        }else {
                  //獲取輸入法是否要顯示的狀態,注意別覺得能夠使用mEdittext.getVisibility()來替代,其實是不行的,
                  //由於OnGlobalLayoutListener的監聽是連續觸發而且有延遲,很容易在mEdittext顯示的一瞬間就隱藏了
                            if ((boolean)mEdittext.getTag()){
                                mEdittext.setVisibility(View.GONE);
                                mEdittext.setTag(false);

                            }
                            L.e("沒有觸發鍵盤");


                        }
                    }
                };
        mRootLayout.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener);//給xml裏的根佈局設置監聽

    }

點擊後顯示輸入框

 mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mEdittext.setFocusable(true);
                mEdittext.setFocusableInTouchMode(true);
                mEdittext.requestFocus();
                InputMethodManager inputManager = (InputMethodManager) mCommentEdittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);//獲取輸入法管理
                inputManager.showSoftInput(mEdittext, 0);//要顯示輸入法的view
                mEdittext.setTag(true);//給輸入框設一個標記,標示輸入框已經顯示
            }
        });

最後注意移除監聽佈局

@Override
    public void onPause() {
        super.onPause();
        mRootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(mGlobalLayoutListener);
    }

 

 

 

 

 

 

 

 

endspa

相關文章
相關標籤/搜索