【问题标题】:Moving the hint of EditText to top in Android Java [duplicate]在Android Java中将EditText的提示移到顶部[重复]
【发布时间】:2020-11-24 14:24:01
【问题描述】:

如何在输入时将 EditText (Android Java) 的提示移动到视图顶部? 我附上了 2 张图片来展示我想如何实现我的 EditText 视图。

如何让search country按照图片在视图顶部提示?

请帮忙!

【问题讨论】:

    标签: android android-studio android-layout android-edittext


    【解决方案1】:

    使用TextInputLayout 可以实现这一点。

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/searchCountriesInputLayout"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    
        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/searchCountriesEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Search Countries" />
    
    </com.google.android.material.textfield.TextInputLayout>
    

    build.gradle:

    dependencies {
        implementation 'com.google.android.material:material:1.1.0'
    }
    

    【讨论】:

    • @RaghavAmbashta:你试过了吗?
    • 我试过了,但显示错误。
    • 错误说明了什么?
    • 应用每次都会崩溃
    • 好的,把logcat的内容贴出来看看有什么问题。
    【解决方案2】:

    您可以为此使用 TextInputLayout:-

    <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/tilSearchCountry"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/padding_normal"
            android:hint="@string/your_hint"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">
    
            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/etSearchCountry"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
        </com.google.android.material.textfield.TextInputLayout>
    

    如果您还没有包含 google 材料库,请将其添加到您的 build.gradle:

    dependencies {
        implementation 'com.google.android.material:material:1.1.0'
    }
    

    【讨论】:

    • @+id/tvHeader 是什么?
    • 对不起@RaghavAmbashta 这是我的错我试图在我的一个布局中使用它以便为您提供解决方案并最终得到那个标签我已经更新了答案
    • 还需要更新@style 主题为 Theme.MaterialComponents.NoActionBar 和 android:layout_margin="@dimen/padding_normal" 显示错误
    • padding_normal 只是您可能在 dimens.xml 中定义的标准填充,如果不只是使用 16dp 代替或根据您的要求,这只是应用程序中填充的标准命名约定,请不要混淆它根据您的要求使用填充。
    猜你喜欢
    • 1970-01-01
    • 2012-05-17
    • 2013-04-14
    • 2013-01-08
    • 2014-08-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 2015-08-24
    相关资源
    最近更新 更多