首先,我在Framelayout中有多个EditText,它们由ScollView包装。
我已经使用下面的代码让ScrollView在键盘显示时向上滚动-
((Activity) getContext()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
此外,我在上面的代码中尝试了ADJUST_PAN,但没有任何变化。
问题是,当我在其中一个EditText中键入时,ScrollView会自动向上滚动,这导致我当前编辑的EditText不可见。
我想知道为什么会发生这种事?在键盘上打字时,我当前编辑的EditText是否失去焦点?我该如何修复它?
顺便说一下,我没有在布局中使用任何xml。所有布局都是用Java编写的。
主要布局为-
public class PoorEditScroll extends ScrollView {
PoorEdit poorEdit;
public PoorEditScroll(Context context) {
super(context);
initUI();
}
public PoorEditScroll(Context context, AttributeSet attrs) {
super(context, attrs);
initUI();
}
private void initUI(){
setScrollContainer(false);
poorEdit = new PoorEdit(getContext());
this.addView(poorEdit);
}
this.addView(poorEdit);
}
public void toJson(String s) {
poorEdit.toJson(s);
}
public void loadJson(String s) {
poorEdit.loadJson(s);
}
class PoorEdit extends LinearLayout {
private EditView editView;
public PoorEdit(Context context) {
super(context);
initUI();
}
public PoorEdit(Context context, AttributeSet attrs) {
super(context, attrs);
initUI();
}
private void initUI(){
this.setOrientation(VERTICAL);
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
this.setLayoutParams(layoutParams);
DisplayMetrics dm = getContext().getResources().getSystem().getDisplayMetrics();
this.setMinimumHeight(dm.heightPixels);
// TODO: 15/11/25 Override OnMeasure in EditView
editView = new EditView(getContext());
this.addView(editView);
((Activity) getContext()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}
public void loadJson(String folderPath){
editView.loadJson(folderPath);
}
public String toJson(String dest){
return editView.toJson(dest);
}
}
而EditView
是-
public class EditView extends FrameLayout implements View.OnDragListener {
public static BaseContainer poorBoy;
private BaseContainer lastAdd = null;
public EditView(Context context) {
super(context);
initUI();
}
public EditView(Context context, AttributeSet attrs) {
super(context, attrs);
initUI();
}
private void initUI(){
this.setOnDragListener(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 1);
this.setLayoutParams(layoutParams);
//Call addView to add a EditText in this function.
addTextOn(0, 0, Constants.eleWidth, Constants.eleHeight);
}
}
在AndroidManifest.xml 中的<activity>
中添加此代码
android:windowSoftInputMode="adjustResize|stateHidden"