最近在做公司项目的时候,要求登录的时候,软键盘不能挡住登录按钮,然后本人百度了半天,各种方法都试过了,尝试了各种失败,以下是网上大神们提供的几种方法的罗列:
- 给Activity配置 android:windowSoftInputMode
- 给布局套加上ScrollView
- RelativeLayout,判断软键盘的显示和隐藏,做不同的动作
AndroidManifest.xml
<activity android:name=".LoginActivity"
android:windowSoftInputMode="adjustResize|stateHidden"
android:configChanges="keyboardHidden|orientation"></activity>
layout_login.xml
<?xml version="1.0" encoding="utf-8"?>
<com.android.demo.widget.ResizeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/new_login_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/com_message_bg">
<ScrollView
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:fillViewport="true"
android:scrollbars="none"
android:fadingEdge="none">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--登陆布局-->
</LinearLayout>
</ScrollView>
</com.android.demo.widget.ResizeLayout>
ResizeLayout.java
public class ResizeLayout extends RelativeLayout{
private static final String TAG = ResizeLayout.class.getSimpleName();
public static final byte KEYBOARD_STATE_SHOW = -3;
public static final byte KEYBOARD_STATE_HIDE = -2;
public static final byte KEYBOARD_STATE_INIT = -1;
private boolean mHasInit;
private boolean mHasKeybord;
private int mHeight;
private onKybdsChangeListener mListener;
public ResizeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public ResizeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ResizeLayout(Context context) {
super(context);
}
/**
* set keyboard state listener
*/
public void setOnkbdStateListener(onKybdsChangeListener listener){
mListener = listener;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (!mHasInit) {
mHasInit = true;
mHeight = b;
if (mListener != null) {
mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
}
} else {
mHeight = mHeight < b ? b : mHeight;
}
if (mHasInit && mHeight > b) {
mHasKeybord = true;
if (mListener != null) {
mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
}
Log.w(TAG, "show keyboard.......");
}
if (mHasInit && mHasKeybord && mHeight == b) {
mHasKeybord = false;
if (mListener != null) {
mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
}
Log.w(TAG, "hide keyboard.......");
}
}
public interface onKybdsChangeListener{
public void onKeyBoardStateChange(int state);
}
}LoginActivity.javaResizeLayout layout = (ResizeLayout) findViewById(R.id.new_login_root);
layout.setOnkbdStateListener(new onKybdsChangeListener() {
public void onKeyBoardStateChange(int state) {
switch (state) {
//软键盘隐藏
case ResizeLayout.KEYBOARD_STATE_HIDE:
break;
//软键盘弹起
case ResizeLayout.KEYBOARD_STATE_SHOW:
mhandler.post(new Runnable() {
@Override
public void run() {
mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
break;
}
}
});
本文介绍了在Android开发中如何处理软键盘弹出时遮挡登录按钮的问题,尝试了包括修改AndroidManifest.xml中Activity的软键盘模式,使用ScrollView,以及自定义ResizeLayout等多种解决方案,并提供了相关代码片段和最终效果展示。
1318

被折叠的 条评论
为什么被折叠?



