繁体   English   中英

键入时,EditText失去焦点

[英]EditText loses focus when typing in it

我以编程方式向滚动视图添加了一堆EditTexts。 当键盘显示出来时,它会调整视图(包括滚动视图)的大小,并且某些edittexts会被它覆盖, 没问题 ,我只是滚动查找它们。 当我输入任何一个编辑文本时, 问题就来了,焦点消失了,要清楚的是视图仍然聚焦但在可见范围之外。 就像键入它会在键盘显示之前重新调整scrollview的大小,因此重新放置了视图。 我已经在网上寻找解决方案,但似乎没有人遇到这个问题。

注意:android:windowSoftInputMode =“ adjustPan”不会解决问题

有什么想法吗? 或我该怎么办?

这是一些代码:

 for (int i = 1; i <= number_of_edittexts; ++i) {
  editText = new EditText(this);
        editText.setInputType(1);
        editText.setImeOptions(6);
        editText.setHint("Example: aeiou");
        editText.setY(heightET);
        editText.setX(widthET);
        editText.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        layout.addView(editText);
        heightTV = heightTV + 100;
        heightET = heightET + 100;
   }

这是XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
tools:context=".FirstActivity"
tools:layout_editor_absoluteY="25dp">


<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="50dp"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="Please insert whatever"
    android:textSize="18sp"
    app:layout_constraintBottom_toTopOf="@+id/scrollView3"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<ScrollView
    android:id="@+id/scrollView3"
    android:layout_width="379dp"
    android:layout_height="0dp"
    android:layout_marginBottom="15dp"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:descendantFocusability="afterDescendants"
    app:layout_constraintBottom_toTopOf="@+id/button4"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView">

    <LinearLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="afterDescendants"
        android:orientation="vertical" />
</ScrollView>

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:onClick="StartNextActivity"
    android:text="Continue"
    android:textSize="18sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/scrollView3" />
</android.support.constraint.ConstraintLayout>

因此,而不是设置与所述视图的高度setY(int)我用setMaxHeight(int)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM