android弹出的软键盘遮挡住部分输入框如下图:
解决这问题试了许多方法,不过没有用,最后解决方法如下:
1.设置该activity 软键盘弹出方式android:windowSoftInputMode="adjustResize"
2.输入框用ScroolView嵌套一层布局
3.在getView中添加监听器
vh.input_et..setOnTouchListener(new View.OnTouchListener() {
@Overridepublic boolean onTouch(View view, MotionEvent motionEvent) {
//这句作用是为了保证EditText文本过多可以滑动(解决与ListView冲突用的)
if ((view.getId() == R.id.input && canVerticalScroll(vh.input_et.getEditText()))) {view.getParent().requestDisallowInterceptTouchEvent(true);
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
view.getParent().requestDisallowInterceptTouchEvent(false);
}
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
index = position;
}
return false;
}
});
/**
EditText滚动的方法(如果不需要这个功能可以省略)
* EditText竖直方向是否可以滚动* @param editText 需要判断的EditText
* @return true:可以滚动 false:不可以滚动
*/
private boolean canVerticalScroll(EditText editText) {
int scrollY = editText.getScrollY();
int scrollRange = editText.getLayout().getHeight();
int scrollExtent =editText.getHeight()-editText.getCompoundPaddingTop() -editText.getCompoundPaddingBottom();
int scrollDifference = scrollRange - scrollExtent;
if(scrollDifference == 0) {
return false;
}
return (scrollY > 0) || (scrollY < scrollDifference - 1);
}
4.在getView结束前为EditText添加焦点
if (index != -1 && index == position) {
// 如果当前的行下标和点击事件中保存的index一致,手动为EditText设置焦点。
vh.input_et.requestFocus();
}
5.但是这样,又会有新的问题出现,EditText中的光标停在最前面,并且不闪动,点击软键盘输入任何东西也不会在EditText中显示,这样,就要在EditText获得焦点后再做一个处理:
editText.setSelection(editText .getText().length());
完成之后的效果图如下
本文参考http://blog.csdn.net/u011213996/article/details/51235386整理