Android輸入框的聯動控制

來源:互聯網
上載者:User

標籤:

/** 
* 對一些含有兩個輸入框和一個確認按鈕的頁面,比如登入註冊頁面,一般是一個帳號輸入框、一個密碼輸入框、以及一個確認按鈕。
* 點擊第二個輸入框彈出鍵盤時,鍵盤常常會把確認按鈕給遮擋住,這時,我們常常要求將確認按鈕框上移至輸入框之上,以避免遮擋確認按鈕。
* 下面三個類即實現了此種功能,代碼獨立,用法簡單快捷
*
* 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輸入框的聯動控制

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.