【问题标题】:EditText loses focus when typing in itEditText 在输入时失去焦点
【发布时间】:2018-09-20 02:21:16
【问题描述】:

我以编程方式将一堆 EditTexts 添加到滚动视图中。当键盘出现时,它会调整视图的大小,包括滚动视图,并且一些编辑文本会被它覆盖,没问题,我只是滚动找到它们。 问题在我输入任何编辑文本时出现,焦点丢失,需要明确的是视图仍然集中但超出了可见范围。这就像在键盘出现之前输入它会再次调整滚动视图的大小,因此,视图被重新分配。我已经在网上寻找解决方案,但似乎没有人遇到这个问题。

注意: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>

【问题讨论】:

    标签: android input android-edittext focus


    【解决方案1】:

    所以我没有使用setY(int) 设置视图的高度,而是使用setMaxHeight(int)

    【讨论】:

      猜你喜欢
      • 2017-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多