/** * 对一些含有两个输入框和一个确认按钮的页面,比如登录注册页面,一般是一个账号输入框、一个密码输入框、以及一个确认按钮。* 点击第二个输入框弹出键盘时,键盘常常会把确认按钮给遮挡住,这时,我们常常要求将确认按钮框上移至输入框之上,以避免遮挡确认按钮。* 下面三个类即实现了此种功能,代码独立,用法简单快捷** 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; } }
时间: 2024-10-12 08:29:17