登录界面经常会出现输入框被键盘遮挡的情况,这里处理的方式是当软件盘出现的时候在界面的最底部空出键盘的高度。需要代码和xml布局一起修改来实现。
1、xml布局通过ScrollView包裹住内容,内容使用LinearLayout包裹。
<?xml version="1.0" encoding="utf-8"?>
<NonFocusingScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/sv_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context="cn.genomics.capture.view.LoginActivity"
>
<LinearLayout
android:id="@+id/view_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical"
>
<ImageView
android:layout_width="@dimen/space_80dp"
android:layout_height="@dimen/space_80dp"
android:layout_marginTop="@dimen/space_50dp"
android:background="@drawable/bg_login"
android:padding="@dimen/space_15dp"
android:src="@mipmap/ic_login_logo"
android:text="登录"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/space_15dp"
android:text="MyBGI"
android:textColor="@color/colorPrimary"
android:textSize="@dimen/text_size_16sp"
android:textStyle="bold"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="51dp"
android:layout_marginTop="@dimen/space_30dp"
android:paddingLeft="33.5dp"
android:paddingRight="33.5dp">
<cn.genomics.capture.view.widget.ClearEditText
android:id="@+id/phone_number"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@null"
android:hint="@string/prompt_user"
android:imeActionId="@+id/login"
android:imeOptions="actionUnspecified"
android:inputType="text"
android:singleLine="true"
android:textColor="@color/colorPrimary"
android:textColorHint="@color/text_color_item_hint"
android:textSize="@dimen/text_size_14sp"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="@color/text_color_item_hint"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="51dp"
android:layout_marginTop="@dimen/space_10dp"
android:paddingLeft="33.5dp"
android:paddingRight="33.5dp">
<cn.genomics.capture.view.widget.ClearEditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@null"
android:hint="@string/prompt_password"
android:inputType="textPassword"
android:singleLine="true"
android:textColor="@color/colorPrimary"
android:textColorHint="@color/text_color_item_hint"
android:textSize="@dimen/text_size_14sp"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentBottom="true"
android:background="@color/text_color_item_hint"/>
</RelativeLayout>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="@dimen/space_120dp"
android:layout_marginTop="@dimen/space_40dp"
android:background="@drawable/btn_login"
android:gravity="top|center_horizontal"
android:paddingTop="38dp"
android:text="@string/action_sign_in"
android:textColor="@color/white"/>
</LinearLayout>
</NonFocusingScrollView>
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);
}
}
});
}
}
到这里完美解决键盘遮挡问题,代码也特别的简单。