EditText with a permanent hint in Java

2019-08-08 20:54发布

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);
}

2条回答
闹够了就滚
2楼-- · 2019-08-08 21:28

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.

查看更多
干净又极端
3楼-- · 2019-08-08 21:28

You can directly do it through xml tag which you've added in your UI layout.

查看更多
登录 后发表回答