键入时ScrollView向上滚动

时间:2015-11-26 07:47:37

标签: android android-edittext scrollview android-input-method

首先,我在Framelayout中有多个EditTexts,它们由ScollView包装。

我已经使用下面的代码让ScrollView在显示键盘时向上滚动 -

((Activity) getContext()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

另外,我在上面的代码中尝试了ADJUST_PAN,没有任何改变。

问题是 - 当我在其中一个EditTexts中输入时,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);
}
}

1 个答案:

答案 0 :(得分:1)

在AndroidManifest.xml中的<activity>

中添加此代码
android:windowSoftInputMode="adjustResize|stateHidden"