標籤:
/**
* 對一些含有兩個輸入框和一個確認按鈕的頁面,比如登入註冊頁面,一般是一個帳號輸入框、一個密碼輸入框、以及一個確認按鈕。
* 點擊第二個輸入框彈出鍵盤時,鍵盤常常會把確認按鈕給遮擋住,這時,我們常常要求將確認按鈕框上移至輸入框之上,以避免遮擋確認按鈕。
* 下面三個類即實現了此種功能,代碼獨立,用法簡單快捷
*
* 1. 一個輸入框時:
* new SoftInputOneEdtCtrl(your FragmentActivity).register(View viewBaseline, View viewToMove, EditText editText);
* 2. 兩個輸入框時:
* new SoftInputTwoEdtCtrl(your FragmentActivity).register(View viewBaseline, View viewToMove, EditText edt1, EditText edit2);
*/
import android.graphics.Rect;import android.os.Handler;import android.support.v4.app.FragmentActivity;import android.view.View;import android.view.View.OnLayoutChangeListener;import android.view.ViewTreeObserver.OnGlobalLayoutListener;import android.widget.EditText;public abstract class SoftInputCtrlAbs implements OnLayoutChangeListener, OnGlobalLayoutListener, Runnable { protected FragmentActivity mActivity; private volatile int mNewBottom; private volatile int mOldBottom; protected View mViewBaseline; protected View mViewToMove; protected volatile boolean mHasMovedUp = false; private Handler mHandler = new Handler(); public SoftInputCtrlAbs(FragmentActivity activity) { mActivity = activity; View rootView = mActivity.getWindow().findViewById(android.R.id.content); rootView.addOnLayoutChangeListener(this); rootView.getViewTreeObserver().addOnGlobalLayoutListener(this); } public void register(View viewBaseline, View viewToMove, EditText... editTexts) { mViewToMove = viewToMove; mViewBaseline = viewBaseline; } public void unregister() { if (mActivity != null) { View rootView = mActivity.getWindow().findViewById(android.R.id.content); rootView.removeOnLayoutChangeListener(this); rootView.getViewTreeObserver().removeGlobalOnLayoutListener(this); } reset(); } public void postMove() { if (mHandler != null) { mHandler.post(this); } } @Override public void run() { moveUp(); } @Override public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) { mNewBottom = bottom; mOldBottom = oldBottom; } public int getNewBottom() { return mNewBottom; } public int getOldBottom() { return mOldBottom; } protected synchronized void moveUp() { if (mViewToMove == null || mViewBaseline == null) return; int[] locationViewBaseline = new int[2]; mViewBaseline.getLocationOnScreen(locationViewBaseline); int[] locationViewToMove = new int[2]; mViewToMove.getLocationOnScreen(locationViewToMove); Rect rectViewToMove = new Rect(); mViewToMove.getGlobalVisibleRect(rectViewToMove); int moveTopDistance = rectViewToMove.bottom - locationViewBaseline[1] - mViewBaseline .getHeight(); if (moveTopDistance > 0 || mHasMovedUp) return; if (moveTopDistance < 0) { mHasMovedUp = true; } mViewToMove.setPadding(mViewToMove.getPaddingLeft(), moveTopDistance, mViewToMove.getPaddingRight(), mViewToMove.getPaddingBottom()); } public void reset() { mViewToMove = null; mViewBaseline = null; mNewBottom = 0; mOldBottom = 0; mHasMovedUp = false; mActivity = null; } public void resetPosition () { if (mViewToMove != null) { mViewToMove.setPadding(mViewToMove.getPaddingLeft(), 0, mViewToMove.getPaddingRight(), mViewToMove.getPaddingBottom()); mHasMovedUp = false; } }}
import android.support.v4.app.FragmentActivity;import android.view.View;import android.widget.EditText;public class SoftInputOneEdtCtrl extends SoftInputCtrlAbs { public SoftInputOneEdtCtrl(FragmentActivity activity) { super(activity); } @Override public void register(View viewBaseline, View viewToMove, EditText... editTexts) { super.register(viewBaseline, viewToMove, editTexts); } @Override public void unregister() { super.unregister(); } @Override public void onGlobalLayout() { int bottom = getNewBottom(); int oldBottom = getOldBottom(); if (bottom < oldBottom) { postMove(); } else if (bottom > oldBottom) { resetPosition(); } }}
import android.support.v4.app.FragmentActivity;import android.view.KeyEvent;import android.view.View;import android.view.View.OnFocusChangeListener;import android.view.inputmethod.EditorInfo;import android.widget.EditText;import android.widget.TextView;import android.widget.TextView.OnEditorActionListener;public class SoftInputTwoEdtCtrl extends SoftInputCtrlAbs{ private static String TAG = "SoftInputTwoEdtCtrl"; private EditText mEdtFirst; private EditText mEdtSecond; private boolean mEdtActionNext = false; private boolean mEdtSecondHasFocus = false; private OnEditorActionListener mOnEditorActionListener = new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_NEXT) { performActionNextEdtFirst(); } return false; } }; public SoftInputTwoEdtCtrl(FragmentActivity activity) { super(activity); } private synchronized void performActionNextEdtFirst() { mEdtActionNext = true; postMove(); } @Override public void register(View viewBaseline, View viewToMove, EditText... editTexts) { super.register(viewBaseline, viewToMove, editTexts); if ( editTexts == null|| editTexts.length != 2) return; mEdtFirst = editTexts[0]; mEdtSecond = editTexts[1]; mEdtActionNext = false; mEdtSecondHasFocus = false; mEdtSecond.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { mEdtSecondHasFocus = hasFocus; postMove(); } }); mEdtFirst.setOnEditorActionListener(mOnEditorActionListener); } @Override public void onGlobalLayout() { int bottom = getNewBottom(); int oldBottom = getOldBottom(); if (bottom < oldBottom) { if (!mEdtSecondHasFocus) return; postMove(); } else if (bottom > oldBottom) { resetPosition(); } else if (mEdtActionNext && mEdtSecondHasFocus) { mEdtActionNext = false; } } @Override public void moveUp() { super.moveUp(); } @Override public void reset() { super.reset(); mEdtFirst = null; mEdtSecond = null; mEdtActionNext = false; mEdtSecondHasFocus = false; }}
Android輸入框的聯動控制