简体   繁体   中英

EditText loses focus when typing in it

I added a bunch of EditTexts programatically to a scrollview. When the keyboard shows up it resize the views, including the scrollview, and some of the edittexts get covered by it, no problem , I just scroll to find them. The problem comes when I type in any of the edittexts, the focus is lost, to be clear the view is still focused but out of the visible range. It's like typing in it would resize again the scrollview before the keyboard showed up and therefore, the view is realocated. I already look for solutions in the web, but nobody seems to have this issue.

Note: android:windowSoftInputMode="adjustPan" won't do the trick

Any idea of what is happening ? or what can I do?

Here is some of the code:

 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;
   }

Here is the 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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