【问题标题】:How to move all elements in Constraint Layout Up on focus in Edit text Field?如何将约束布局中的所有元素向上移动到编辑文本字段中的焦点?
【发布时间】:2019-05-06 02:37:13
【问题描述】:

我正在尝试为电子邮件请求制作带有“编辑文本”字段的布局。该设计使键盘始终在 editText 视图上打开,这使得您无法知道您正在输入什么。我已经尝试了许多关于堆栈溢出的建议,但其中大多数是针对 API

在 Activity 文件中我尝试了以下操作:

window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN or WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)

window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)

window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN or WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)


window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)

在 Android Manifest 中,我还尝试了以下结合和单独的方法:

android:windowSoftInputMode="adjustPan"
android:windowSoftInputMode="stateHidden|adjustPan"
android:windowSoftInputMode="adjustResize"

我还按照 Heena 的描述将布局包装在 ScrollView 中 这里:https://stackoverflow.com/a/39867438/5635144

以下是我的 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"
    tools:context=".EmailResetActivity">


    <RelativeLayout
        android:id="@+id/bg"
        android:layout_width="match_parent"
        android:layout_height="419dp"
        android:background="@drawable/signin_up_anim"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            ...
        </android.support.constraint.ConstraintLayout>

    </RelativeLayout>


    <EditText
        android:id="@+id/e_request"
        android:layout_width="305dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="48dp"
        android:layout_marginEnd="8dp"
        android:ems="10"
        android:hint="@string/email_address"
        android:inputType="textEmailAddress"
        android:paddingStart="10dp"
        android:paddingBottom="15dp"
        android:textColorHint="@color/hintgrey"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.522"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/bg" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:id="@+id/buttonsContainer"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/e_request">

        <Button
            android:id="@+id/sendRequestBtn"
            android:layout_width="150dp"
            android:layout_height="45dp"
            android:background="@drawable/large_btn"
            android:text="@string/submit"
            android:textColor="@color/white" />
    </LinearLayout>


</android.support.constraint.ConstraintLayout>

class EmailResetActivity : AppCompatActivity() {
    lateinit private var auth: FirebaseAuth

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_email_reset)
        window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)

        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN or WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)

        auth = FirebaseAuth.getInstance()



        val animDrawable = resetPassInfoBg.background as AnimationDrawable
        animDrawable.setEnterFadeDuration(2500)
        animDrawable.setExitFadeDuration(1500)
        animDrawable.start()


        sendRequestBtn.setOnClickListener(View.OnClickListener {
            val email = e_request.getText().toString().trim()

            if (TextUtils.isEmpty(email)) {
                Toast.makeText(application, "Enter your email ", Toast.LENGTH_SHORT).show()
                return@OnClickListener
            }


        })


    }

}

我希望将布局向上移动,以便可以看到 editText 字段,但目前没有任何东西可以让它工作。

【问题讨论】:

  • 删除LinearLayout 怎么样?
  • 我刚刚在 API 19 和 API 26 (Android 8.0) 上尝试了您的布局。它适用于两种设备。无需更改清单中的任何内容。 (???)。你能分享你的活动吗?
  • 我已添加它以供您注意。我不知道为什么它根本无法在我的设备上运行。
  • 好的,看我的回答。

标签: android android-layout kotlin onfocus


【解决方案1】:

删除这一行:

window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)

对于FLAG_LAYOUT_NO_LIMITS,应用程序无法计算如何滚动您的视图,因为您的布局没有限制。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    • 1970-01-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    相关资源
    最近更新 更多