简体   繁体   English

键入时,EditText失去焦点

[英]EditText loses focus when typing in it

I added a bunch of EditTexts programatically to a scrollview. 我以编程方式向滚动视图添加了一堆EditTexts。 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. 当键盘显示出来时,它会调整视图(包括滚动视图)的大小,并且某些edittexts会被它覆盖, 没问题 ,我只是滚动查找它们。 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. 就像键入它会在键盘显示之前重新调整scrollview的大小,因此重新放置了视图。 I already look for solutions in the web, but nobody seems to have this issue. 我已经在网上寻找解决方案,但似乎没有人遇到这个问题。

Note: android:windowSoftInputMode="adjustPan" won't do the trick 注意:android:windowSoftInputMode =“ adjustPan”不会解决问题

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:

<?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