【问题标题】:ScrollView scroll up when typingScrollView 打字时向上滚动
【发布时间】:2016-02-29 05:20:08
【问题描述】:

首先,我在一个由 ScollView 包裹的 Framelayout 中有多个 EditText。

我已经使用下面的代码让 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);
}
}

【问题讨论】:

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


    【解决方案1】:

    将此代码添加到 AndroidManifest.xml 中的 <activity>

    android:windowSoftInputMode="adjustResize|stateHidden"
    

    【讨论】:

    • 不工作...我已经尝试过adjustResize和adjustPan。键盘显示时编辑视图可见,但在键盘上键入时,滚动视图将向上滚动并覆盖我的编辑视图。无论如何感谢您的回答。
    • 尝试使用xml文件进行设计。我从未尝试过使用 java 的滚动视图
    猜你喜欢
    • 2013-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    • 2021-09-03
    • 2015-02-02
    • 1970-01-01
    相关资源
    最近更新 更多