如何使编辑文本提示出现在输入时

5

我希望我的应用程序能够在不同的时间显示不同的提示文本,因此我需要根据需要编程设置提示文本,如下所示。

editText.setHint("Hint 1");

但问题在于当我开始输入时,提示文本就会消失。我想每次都显示这个提示。如果我在 XML 中设置了提示,那么在输入时它将不会消失,但是在我的情况下如何实现呢?


2
如果我正确理解了你的问题,你应该使用TextInputLayout。在你的布局xml文件中,将你的EditText放在TextInputLayout里面。 - SripadRaj
@SripadRaj 是的,我有TextInputLayout来包含EditText。我的问题是,当我根据条件动态更改提示时,当用户在EditText上键入任何内容时,提示会消失。我希望始终显示此提示。 - KJEjava48
没有使用 TextInputLayout 是可能的吗? 实际上,我已经通过编程方式设置了提示,并且它运行良好。 但是现在我想以同样的方式设置文本,问题是在设置文本后提示会隐藏。 - Arbaz.in
@Arbaz.in,Ravi Sahu的答案看起来不错。我还没有尝试过。如果您想尝试,请尝试一下。 - KJEjava48
6个回答

4
要么你应该考虑改用TextInputLayout(强烈推荐使用。 这里也有一个示例。
要么就创建一个相对布局,其中包含EditText输入和TextView提示。根据需要更改文本视图。
<RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Some text"
            android:layout_alignParentLeft="true"
            android:id="@+id/editText1"
            android:layout_gravity="center"
            />
    <TextView
            android:id="@+id/text_hint"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:text="yourHint"
            android:textSize="14sp"
            android:layout_marginRight="10dp"
            android:textColor="@color/customColor"
            />
</RelativeLayout>

我喜欢你提出的第一种方法,但问题是提示将显示在EditText底部,一旦您清除输入的文本,然后再次输入任何文本,错误将不会显示为提示。 - KJEjava48

2

很抱歉,您期望的不可能实现。因为我们必须知道用户在EditText中输入了什么内容。 近年来,类似https://github.com/hardik-trivedi/FloatingLabel这种提示方式更加流行,您可以尝试使用以上链接。


1

1
在Android中,当您在编辑文本的提示中输入文本时,无法显示文本。默认情况下,具有此功能。
<android.support.design.widget.TextInputLayout
        android:theme="@style/TextLabel"
        android:id="@+id/input_layout_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <EditText
            android:id="@+id/username_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@null"
            android:hint="Email Address"
            android:inputType="textEmailAddress"
            android:translationY="10dp"
            android:textColor="@color/white"
            android:textColorHint="@color/white"
            android:textSize="@dimen/font_13"/>
</android.support.design.widget.TextInputLayout>

在Android中,当在EditText中输入文本时,会自动显示在编辑框上方的提示信息。你也可以通过编程方式设置提示信息。
 EditText. setHint ("Hint 1");

如果我在XML中设置提示,那么在编辑文本时,提示将显示在编辑文本上方。如果我这样做,该提示将变为永久性的,我将无法再更改提示。如果我使用EditText.setHint("提示1"),则在编辑文本时输入后,提示将消失。 - KJEjava48


0
    <!--EMAIL-->
<android.support.design.widget.TextInputLayout
    android:id="@+id/email_wrapper"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/TextLabel">

    <EditText
        android:id="@+id/etUserName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/login_screen_email_hint"
        android:imeOptions="actionNext"
        android:inputType="textEmailAddress"
        android:maxLines="1"
        android:nextFocusDown="@+id/etPassword"
        android:singleLine="true"
        android:textColor="@android:color/white"
        android:textColorHighlight="@android:color/white"
        android:textColorHint="@android:color/white" />

</android.support.design.widget.TextInputLayout>

别忘了将这行代码添加到你的Gradle文件中

 compile 'com.android.support:design:23.4.0'

我需要根据下拉选项更改提示。如果我在XML中设置了提示,那么当我更改下拉项时,如何更改提示? - KJEjava48
1
所以你只需要使用支持设计到22.2.1, 编译'com.android.support:design:22.2.1', 和((EditText) findViewById(R.id.etUserName)).setHint('bla bla');在22.2.1之前是一个已知的错误: https://code.google.com/p/android/issues/detail?id=175228&q=TextInputLayout&colspec=ID%20Type%20Status%20Owner%20Summary%20Stars - Lior

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接