Nestscrollview EditText 点击弹出下拉菜单被弹出软键盘遮挡
找了一圈没有适合自己的,谈思路 网上很多 但是不一定适合自己 要思考自己的问题点
1.Mainfest android:windowSoftInputMode=“adjustPan”
1.监听软键盘弹出
private void restoreEditText() {
NestedScrollView scrollView = findViewById(R.id.nestedScrollView); // 根据实际情况获取 ScrollView 或 NestedScrollView 对象
int scrollY = (int) etSysName.getY(); // 获取 EditText 在布局中的相对位置
scrollView.smoothScrollTo(0, -scrollY); // 滚动到 EditText 的位置
}
private void scrollToEditText() {
NestedScrollView scrollView = findViewById(R.id.nestedScrollView); // 根据实际情况获取 ScrollView 或 NestedScrollView 对象
int scrollY = (int) etSysName.getY(); // 获取 EditText 在布局中的相对位置
scrollView.smoothScrollTo(0, scrollY); // 滚动到 EditText 的位置
}
// 解决系统名称软键盘弹出下拉菜单被遮挡问题
private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener;
private void adjustSysDropDown(){
keyboardLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
private int previousHeight = 0;
@Override
public void onGlobalLayout() {
Rect r = new Rect();
etSysName.getWindowVisibleDisplayFrame(r);
int screenHeight = etSysName.getRootView().getHeight();
int keypadHeight = screenHeight - r.bottom;
if (keypadHeight > screenHeight * 0.15) {
// 软键盘显示
if (keypadHeight != previousHeight) {
previousHeight = keypadHeight;
// 执行相应的操作,比如滚动 EditText 或调整布局
scrollToEditText();
}
} else {
// 软键盘隐藏
if (previousHeight != 0) {
previousHeight = 0;
// 执行相应的操作,比如将 EditText 恢复到原始位置
// restoreEditText();
}
}
}
};
etSysName.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);
}
```