简体   繁体   中英

EditText with a permanent hint in Java

I have an EditText with some information in it. When there is no information, I'm displaying a caption using setHint() .

final EditText info = new EditText(activity);
if (data.getinfo() != null && !data.getAddress().trim().equals("")) {
    info.setText(data.getinfo());
} else {
    info.setHint(R.string.info);
}

I want to show that caption even if the EditText has info, the info should be below the caption.

I tried using setText() . But that didn't work either.

if (condition) {
    info.setText(R.string.info);
    info.setText(data.getinfo());
} else {
    info.setHint(R.string.info);
}

Within a RelativeLayout , use a TextView as an overlay View on top of your EditText . Use appropriate colors, size for the font to achieve the effect you are looking for.

Here is a hint

<RelativeLayout …>
    <EditText android:id="@+id/name" 
        android:layoutWidth="match_parent"
        android:layoutHeight="wrap_content" >

    <TextView android:id="@+id/name_hint" 
        android:layoutWidth="wrap_content"
        android:layoutHeight="wrap_content"
        android:gravity="left|center_vertical"
        android:layout_alignLeft="@id/name"
        android:layout_alignRight="@id/name"
        android:layout_alignTop="@id/name"
        android:layout_alignBottom="@id/name"
        android:layout_alignBottom="@id/name" >
</RelativeLayout>

This is just a starting point, you can adjust the height of your EditText , position your TextView , change its gravity , etc., to achieve what you are looking for.

您可以通过添加到UI布局中的xml标签直接进行操作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM