一、先看在沉浸式模式中,什么都不处理的。只是添加2个EditText
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="300dp"
android:hint="请输入内容"/>
<EditText
android:id="@+id/input_edit_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入地址"/>
<activity
android:name=".InputManagerActivity"
android:label="@string/title_activity_input_manager"
android:windowSoftInputMode="adjustPan"
android:theme="@style/AppTheme">
</activity>
显示效果如下:
RE:上面这种方式,如果有多个输入框,下面的就没法显示,必须隐藏输入框,然后再点击。
2.根据代码判断是否弹出软键盘,移动界面的位置。
public class InputManagerActivity extends AppCompatActivity {
private LinearLayout content_input_manager;
EditText input_edit_two;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input_manager);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
content_input_manager = (LinearLayout) findViewById(R.id.content_input_manager);
input_edit_two = (EditText) findViewById(R.id.input_edit_two);
addLayoutListener(content_input_manager, input_edit_two);
}
//这种方式是通过代码,直接移动布局。移动的高度也位置,根据你传入的View(比如当前的是input_edit_two)确定
private void addLayoutListener(final View view, final View scroll) {
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
//获取content_input_manager在窗口的可视区域
view.getWindowVisibleDisplayFrame(rect);
//view.getRootView().getHeight()为总高度(不变),rect.bottom为当前可视区域的底部(会发生变化)
//如果没有软键盘弹出,view.getRootView().getHeight() 和 rect.bottom是相等的
int inVisableHeight = view.getRootView().getHeight() - rect.bottom;
if (inVisableHeight > 100) {
int[] location = new int[2];
scroll.getLocationInWindow(location);//获取input_edit_two的窗体坐标
int scrollHeight = (location[1] + scroll.getHeight()) - rect.bottom;
view.scrollTo(0, scrollHeight);//这个最终目的是让input_edit_two的底部和软键盘的顶部对齐。也就是input_edit_two显示在可视区域的底部
} else {
view.scrollTo(0, 0);
}
}
});
}
}
效果图如下: