android,键盘遮挡了输入框,完美解决android键盘遮挡问题

登录界面经常会出现输入框被键盘遮挡的情况,这里处理的方式是当软件盘出现的时候在界面的最底部空出键盘的高度。需要代码和xml布局一起修改来实现。


1、xml布局通过ScrollView包裹住内容,内容使用LinearLayout包裹。




    

        

        

        

            

            
        

        

            

            
        

        


其中 NonFocusingScrollView 是自定义的ScrollView用来让ScrollView不自动切换子view的焦点

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.ScrollView;

/**
* 自定义ScrollView,不让ScrollView自动切换子view的焦点
* @author chentuanhui
* created at 2017/4/28 11:18 
**/
public class NonFocusingScrollView extends ScrollView {

  public NonFocusingScrollView(Context context) {
    super(context);
  }

  public NonFocusingScrollView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public NonFocusingScrollView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
  }

  /**
   * TODO 不让ScrollView自动切换子View的焦点
   * @param direction
   * @param previouslyFocusedRect
   * @return
   */
  @Override
  protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
    return true;
  }

}

activity对应的java代码

public class LoginActivity extends AppCompatActivity implements OnClickListener {
    private EditText mUserNameEt;
    private EditText mPasswordEt;
    private Button mLoginBtn;
    private LoginPresenter mPresenter;
    private long mTime;
    private Handler mHandler = new Handler();
    private ScrollView mContainerSv;
    private View mContentView;
    private View mContainer;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContainer = LayoutInflater.from(this).inflate(R.layout.activity_login, null);
        setContentView(mContainer);
        initView();
        setListener();
    }

   
    protected void initView() {
        mUserNameEt = (EditText) findViewById(R.id.phone_number);
        mPasswordEt = (EditText) findViewById(R.id.password);
        mLoginBtn = (Button) findViewById(R.id.btn_login);
        mContainerSv = (ScrollView) findViewById(R.id.sv_content);
        mContentView = findViewById(R.id.view_content);
    }

    protected void setListener() {
        mLoginBtn.setOnClickListener(this);
        mContainer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                mContainer.getWindowVisibleDisplayFrame(r);
                int screenHeight = mContainer.getRootView().getHeight();

                // r.bottom is the position above soft keypad or device button.
                // if keypad is shown, the r.bottom is smaller than that before.
                int keypadHeight = screenHeight - r.bottom;

                if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
                    // 当键盘显示的时候走这里,最底部空出键盘占用的高度
                    mContentView.setPadding(0, 0, 0, keypadHeight);
                    //延迟滚动到底部,为了防止焦点出现跳动
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            //将ScrollView滚动到最底部
                            mContainerSv.fullScroll(View.FOCUS_DOWN);
                        }
                    }, 100);
                } else {
                    // 当键盘隐藏的时候走这里,还原默认的显示
                    mContentView.setPadding(0, 0, 0, 0);
                }
            }
        });
    }
}



到这里完美解决键盘遮挡问题,代码也特别的简单。

android,键盘遮挡了输入框,完美解决android键盘遮挡问题_第1张图片

android,键盘遮挡了输入框,完美解决android键盘遮挡问题_第2张图片



你可能感兴趣的:(android,键盘遮挡)