Android EditText悬浮在输入法之上
Android EditText悬浮在输入法之上
使用
android:windowSoftInputMode="adjustResize"会让界面整体被顶上去,很多时候我们不需要这样的情况出现,这里给出另一个方案.
**思路:监听输入法的状态,然后动态的滚动
EditText所在的ViewGroup或者View**
1. Android Manifest.xml
2. 布局文件
3. Activity里设置监听,滚动 input 视图
public class InputActivity extends AppCompatActivity {
private RelativeLayout rLayout;
private View mInputLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_input);
mInputLayout = findViewById(R.id.rl_inputdlg_view);
rLayout = ((RelativeLayout) findViewById(R.id.root));
//输入法到底部的间距(按需求设置)
final int paddingBottom = DisplayUtil.dp2px(this, 5);
rLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
rLayout.getWindowVisibleDisplayFrame(r);
//r.top 是状态栏高度
int screenHeight = rLayout.getRootView().getHeight();
int softHeight = screenHeight - r.bottom ;
Log.e("test","screenHeight:"+screenHeight);
Log.e("test","top:"+r.top);
Log.e("test","bottom:"+r.bottom);
Log.e("test", "Size: " + softHeight);
if (softHeight>100){//当输入法高度大于100判定为输入法打开了
rLayout.scrollTo(0, softHeight+paddingBottom);
}else {//否则判断为输入法隐藏了
rLayout.scrollTo(0, paddingBottom);
}
}
});
}
}
效果图:
1. 不做处理前:

2. 处理后的效果:


浙公网安备 33010602011771号