Android EditText键盘使片段消失

时间:2017-10-25 00:31:05

标签: android

在我的应用程序中的一个片段中,我有一个edittext,每当我点击它时,键盘就会出现,片段中的所有内容都会消失,包括edittext。我仍然可以在键盘上打字,按下后退按钮后,一切都会回来,我在编辑文本上显示的内容,但是如何阻止所有内容消失?

以下是片段及其布局的代码。

片段:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class PaymentsFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_payments, container, false);
    }
}

布局:

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

android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
    android:id="@+id/editText2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"

    android:hint="Enter Something"

    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/button2"

    android:layout_marginLeft="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginRight="16dp" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    android:text="Button"

    app:layout_constraintBaseline_toBaselineOf="@+id/editText2"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toRightOf="@+id/editText2"

    android:layout_marginLeft="0dp"
    android:layout_marginRight="16dp" />

</android.support.constraint.ConstraintLayout>

3 个答案:

答案 0 :(得分:2)

参考:Developer documentation

  

<强>机器人:windowSoftInputMode

     

对活动的主窗口进行了调整 - 是否是   调整大小调整为软键盘腾出空间或是否为它   内容平移以使当前焦点在窗口的一部分可见   被软键盘覆盖。

     

<强> adjustResize

     

活动的主窗口总是调整大小以便为软件腾出空间   键盘在屏幕上。

     

<强> adjustPan

     

活动的主窗口未调整大小以便为软件腾出空间   键盘。相反,窗口的内容会自动平移   这样当前的焦点永远不会被键盘和用户遮挡   总能看到他们正在打字的东西。这通常不太理想   比调整大小,因为用户可能需要关闭软键盘   进入并与窗户的模糊部分互动。

在清单中使用这种方式

<activity android:windowSoftInputMode="adjustResize"> </activity>

答案 1 :(得分:0)

即使在Android 8上,在“ ConstraintLayout中片段的Scrollview中的多行EditText中”的情况下,我仍然会重现它。使用SingleLine不会产生任何问题。

之所以发生这种情况,是因为EditText的动态高度在这种情况下不能很好地工作。 此解决方案有些破绽,但如果您有能力固定行数,它的确可以像魅力一样发挥作用:

  • 创建扩展EditText的自定义类LabelEditView
  • 添加属性
  <declare-styleable name="LabelEditView">
    <attr name="label_edit_line_count" format="integer" />
  </declare-styleable>
  • 在构造函数调用中:
setLineCount(attributes.getInt(R.styleable.LabelEditView_label_edit_line_count,
     

1));

然后,最后是有趣的代码:

  /**
   * Set the number of visible lines. Does not support to switch between modes or font
   */
  public void setLineCount(int lineCount) {
    if (lineCount <= 1) {
      setSingleLine(true);
    } else {
      setSingleLine(false);
      setImeOptions(EditorInfo.IME_ACTION_DONE);
      setRawInputType(InputType.TYPE_CLASS_TEXT);

      // Fix around EditText in Scrollview in fragment in ConstraintLayout
      Paint.FontMetrics fm = getPaint().getFontMetrics();
      int textHeight = (int) ((fm.bottom - fm.top) * (lineCount + 0.5));
      setMaxHeight(textHeight);
      setHeight(textHeight);
    }
  }

答案 2 :(得分:-1)

放入AndroidManifest.xml(在您的活动中):

<activity android:name="(activity name)" android:windowSoftInputMode="adjustPan">
相关问题